Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:06:49+00:00 2026-05-23T19:06:49+00:00

#include <iostream> #include <vector> #include <cassert> class a_class { public: int num_IN; a_class():num_IN(0){} a_class(a_class

  • 0
#include <iostream>
#include <vector>
#include <cassert>

class a_class
{
public:

    int num_IN;

    a_class():num_IN(0){}
    a_class(a_class const & origin){/*Initialise();*/}   //if not called here, error occurs

    void Initialise(){num_IN =5;}
};

int main () 
{
    std::vector <a_class> the_vector;

    for(int q=0; q < 30; q++)
    {
        the_vector.push_back(a_class());
        the_vector[q].Initialise();             
        assert(5 == the_vector[q].num_IN);      //no problem here
    }

    for(int q=0; q < 30; q++)
        assert(the_vector[q].num_IN == 5);      //assertion fails
}

I don’t understand the difference between calling this from outside vs. inside the CC. I also don’t know why it should cause a problem in any case.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:06:50+00:00Added an answer on May 23, 2026 at 7:06 pm

    std::vector may reallocate the buffer it uses if it’s size outgrows it, in which case it has to copy the old elements over to the new buffer. If you don’t have a proper copy constructor that copies num_IN over, the old value is lost.

    Fix this by providing a proper copy constructor:

    a_class(a_class const & origin) : num_IN(origin.num_IN) {}
    

    In the code posted the copy constructor isn’t even needed – if you don’t provide one the compiler will generate a suitable one here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include iostream #include vector class ABC { private: bool m_b; public: ABC() : m_b(false)
#include iostream #include vector class ABC { }; class VecTest { std::vector<ABC> vec; public:
#include iostream class A { private: int a; public : A(): a(-1) {} int
#include<iostream> using namespace std; class A { int a; int b; public: void eat()
#include<iostream> class name { public: int a; name():a(0){}; }; void add(name * pname) {
#include <iostream> #include <vector> using namespace std; class Base { public: void Display( void
#include <iostream> #include <vector> #include <algorithm> using namespace std; class A { public :
#include <iostream> #include <vector> using namespace std; int main() { vector< vector<int> > dp(50000,
#include <iostream> using namespace std; class Base { public: Base(){cout <<Base<<endl;} virtual ~Base(){cout<<~Base<<endl;} virtual
Why this code gives error: #include<iostream> #include<vector> using namespace std; int main() { char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.