Copy Constructor Help

This is a discussion on Copy Constructor Help within the C++ Programming forums, part of the General Programming Boards category; I am looking for confirmation from all of you C++ gurus that my thinking is correct with regards to the ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    16

    Question Copy Constructor Help

    I am looking for confirmation from all of you C++ gurus that my thinking is correct with regards to the copy constructor.

    Ok, basically the default copy is just a direct memberwise copy operation. Now, in the absence of a copy constructor this is what is performed. If there is a copy constructor, then every time a instance of a class is being assigned to another class variable that has already been constructed, then the copy constructor is called to perform this operation. Is this correct?

    Is my thinking also correct that if there are no pointers, references, or dynamic memory storage of any kind in a class that a copy constructor really isn't necessary?
    Thanks ahead of time

  2. #2
    William
    Guest
    I'm not sure I get what you're saying. This what I know :

    Copy ctor is called when creating a class with another (of the same class) as an argument. operator= is used to copy an existing instance from another. Examples :

    class C
    {
    ...
    };

    C c1; //default ctor
    C c2(c1); //Copy ctor
    c1=c2; //operator=

    C c3=c1; //compiler's choice to take either copy ctor, or default ctor then operator=...

    I do think that a class with no pointers/references/arrays will be copied correctly by the default copy ctor, default operator= is ok to.

    It is common (

    Hope this answers your question....

  3. #3
    William
    Guest
    Sorry, I got cut off there....

    I wanted to add :

    it is common and good practice to use operator= in copy ctor.

    Also, the default copy ctor and operator= do not do a mem copy. Rather, they do a member to member copy. This implies that if an instance class B contains a instance of class B and if the default copy ctor (or operator=) is called on class B then the copy ctor (or operator=) will be called on class A. In the case of a mem copy, this would not be the case....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. copy = concatenate ?
    By Arruba in forum C Programming
    Replies: 3
    Last Post: 11-03-2006, 03:54 PM
  2. calling copy constructor from template
    By Ancient Dragon in forum C++ Programming
    Replies: 3
    Last Post: 09-28-2005, 01:54 PM
  3. dynamic memory alloccation & returning objects
    By haditya in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2005, 11:55 PM
  4. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM
  5. Copy Constructor crashing program
    By bob2509 in forum C++ Programming
    Replies: 5
    Last Post: 11-12-2002, 03:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21