Copy Constructors and the vector class.
Hi I'm learning C++ and I am reading about copy constructors. Well, I have a question:
Why, if I don't provide a proper copy constructor for a class, Will an initialization such as:
vector<ThatClass> v(5);
fail?
I understand that compilers can ignore the copy constructor with arrays or function returns by value, but what's different about the vector class?
Thanks!
Re: Copy Constructors and the vector class.
Quote:
Originally Posted by
xyzw
Hi I'm learning C++ and I am reading about copy constructors. Well, I have a question:
Why, if I don't provide a proper copy constructor for a class, Will an initialization such as:
vector<ThatClass> v(5);
fail?
I understand that compilers can ignore the copy constructor with arrays or function returns by value, but what's different about the vector class?
Thanks!
Because internally, a vector has to copy elements to move them around, or even just to fill the vector. While you as a user might never copy construct the object, the vector requires copy construction for proper use.
Also, be careful about the words "if I don't provide a proper copy constructor". If you don't provide a copy constructor, one will exist by default. It might not be perfect if your object handles memory though.
Re: Copy Constructors and the vector class.
Quote:
Originally Posted by
xyzw
I understand that compilers can ignore the copy constructor with arrays or function returns by value, but what's different about the vector class?
A sure way to make a class NOT work in an STL vector is to put this in the class definition (the .h file),
Code:
private:
ThatClass(const ThatClass&); // class cannot be copied
ThatClass& operator=(const ThatClass&); // nor assigned
and then NOT defining these operators (in the .cpp file).
This will guarantee your class will NOT work in any STL container (which requires classes to be both copyable and assignable)
Re: Copy Constructors and the vector class.
Quote:
Originally Posted by
xyzw
Hi I'm learning C++ and I am reading about copy constructors. Well, I have a question:
Why, if I don't provide a proper copy constructor for a class, Will an initialization such as:
vector<ThatClass> v(5);
fail?
No. The statement will create a vector with initial size 5 and use the default constructor of ThatClass to initiate the 5 objects. A copy constructor of ThatClass will be required, if:- You add elements to the vector after its creation, forcing the vector to reallocate memory
- You copy the vector itself
- More things I cannot think of at the moment
So in general, it is very unwise to use vectors of classes that do not have a working copy constructor (either auto-generated or explicitly specified)
Quote:
Originally Posted by
xyzw
I understand that compilers can ignore the copy constructor with arrays or function returns by value, but what's different about the vector class?
For functions returning values, the compiler may optimize away use of copy constructor, which is very different from it not being used. You must not ever rely on the compiler performing some optimization, your program should be correct without it as well.