Copy Constuctor
-
Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?
-
Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?
The parent constructor is called at the beginning of the initialization, but not necessarily the copy constructor. For a default copy constructor, of course the copy constructor of the base would be called. But if the copy constructor is provided in the derived class explicitly, it can be chosen that which one of the base class's constructor is to be called.
-
Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?
tom groezer wrote:
Does it have to be explicit?
The best advice is to always make it explicit to avoid surprises. It can get mundane for simple classes but helps prevent some of life's little oopsies when your class manages resources. As far as I know, if you explicitly implement the copy constructor, you should call the super class copy constructor first in your initilization list. Also remember to call the super class assignment operator in it's explicit implementation to prevent any surprises. It is best to define the copy constructor and the assignment operator to avoid what may or may not get done by the default implementations. If you have "Effective C++ Third Edition" by Scott Meyers, there is some great, to the point, advice/information on this topic. If you don't have it, it's one of the few books that can justify the $40+ price tag.
-
Imagine a hierarcht of two classes. Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called? Is it like Java and the parent constructor is called at the beginning of each constructor? Does it have to be explicit? How does the base and derived class notion of copy constructor work here?
tom groezer wrote:
Suppose the copy constructor of a derived class is called. When is the base (parent) class copy constructor called?
Why not set a breakpoint in both, run the program, and note which breakpoint is hit first?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne