If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

copy constructor issues with vector class

Hey there. Newbie c++ student here. I am experiment with copy constructors and STL containers. I made a test class A and gave it an assignment operator and a copy constructor. I then instantiated an object of type A and then a vector to hold objects of this class A. Then I used push_back method to insert an instance of A and I get an error in Visual C++ 2010 that breaks to the push_back implementation in vector.cpp.

Object a1 adds just fine into the vector v1, but a2 does not. The problem seems to be with the fact that it was instantiated with my provided 0 argument constructor which assigns the pointer member to nullptr. Here is the output before the debugger breaks:

"
A 2 argument constructor called
A 0 argument constructor called
A copy constructor called
A copy constructor called
A destructor called
"

Now. when I change the default constructor A::A() to assign a memory using "new int" and assign a value to the int, then it adds just fine to the vector and the following output is produced:

"

A 2 argument constructor called
A 0 argument constructor called
A copy constructor called
A copy constructor called
A destructor called
A copy constructor called
"

Something is going on with the pointer in A::A() and I cannot figure out what it is after having read through some copy pointer tutorials. Your debugging help is appreciated. Knowing whats wrong will deeply improve my knowledge of C++

Re: copy constructor issues with vector class

You also have a problem with operator =. Operator= usually returns a reference so that multiple assignments work (eg a1 = a2 = a3 = a4 Therefore you can't return a value which is temp (like tempA) as it no longer exists once the function returns.

Assuming that b is never null (ie set as per my post #3), operator = could be implemented as

Re: copy constructor issues with vector class

Your issue has nothing to do with STL containers. Copying and assigning objects correctly can be done without any containers whatsoever, as Philip shows in his code.

All the STL containers do is make copies. But any C++ that makes copies would see the same issue, so it isn't an STL issue. For example, if you pass that faulty object by value to a function, you get the same issues:

There is no STL here, but you get the same issues occuring due to pass-by-value incurring a copy.

Secondly this:

A 2 argument constructor called
A 0 argument constructor called
A copy constructor called
A copy constructor called
A destructor called
"

Now. when I change the default constructor A::A() to assign a memory using "new int" and assign a value to the int, then it adds just fine to the vector and the following output is produced:

"

A 2 argument constructor called
A 0 argument constructor called
A copy constructor called
A copy constructor called
A destructor called
A copy constructor called
"

Relying on when or where a copy constructor is called by printing messages will just confuse you. The reason why is that the compiler can make 5, 10, 20, or 1,000 copies if it wants to, or 0 copies if it is able to. The end result of the copies (or non-copying) must be certain, but the number of copies is up to the compiler, compiler options, optimizations, etc.. So don't look at two sets of output and say "this output is different than the other output, so something is right/wrong here".

What you need to do is do as Philip did, and that is to create a simple program that tests copying and assignment.

Code:

int main()
{
A a1;
A a2(a1);
A a3;
a3 = a1;
}

Something like the above. The above tests construction, copy construction, assignment, and destruction using 3 objects. If A allocates memory and/or uses pointers, you can easily test/correct your issues with something like this code.

Re: copy constructor issues with vector class

Re: copy constructor issues with vector class

Originally Posted by bigc++

Newbie c++ student here.

As C++ is often taught it's easy to get the impression that writing copy constructors, copy assignment operators and even destructors is very central to C++ programming. It's not. One can in fact argue that these constructs preferrably should be avoided alltogether, especially if you're using the object oriented programming style in general high-level applications. So once you've passed the exam the best you can do is to forget about them. Quite a relief isn't it.