CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2009
    Posts
    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!

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Copy Constructors and the vector class.

    Quote Originally Posted by xyzw View Post
    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.

  3. #3
    Join Date
    May 2009
    Posts
    2,413

    Re: Copy Constructors and the vector class.

    Quote Originally Posted by xyzw View Post
    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)

  4. #4
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Copy Constructors and the vector class.

    Quote Originally Posted by xyzw View Post
    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 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured