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 :)
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 :
C c1; //default ctor
C c2(c1); //Copy ctor
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....
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....