-
July 17th, 2009, 03:45 PM
#1
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!
-
July 17th, 2009, 03:56 PM
#2
Re: Copy Constructors and the vector class.
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.
-
July 17th, 2009, 05:47 PM
#3
Re: Copy Constructors and the vector class.
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)
-
July 18th, 2009, 05:20 AM
#4
Re: Copy Constructors and the vector class.
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)
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.
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|