Implicit copy / assign instance operator behavior

I have a question about the C ++ standard.

Suppose you have a base class with a custom copy constructor and assignment operator. The derived class uses the implicit one generated by the compiler.

Does the copy / assignment of the derived class do the custom instance / assignment constructor? Or do you need to implement user-defined versions that call the base class?

Thank you for your help.

+2


a source to share


2 answers


If the derived class does not declare a copy constructor, but an implicit one, (12.8 / 4 "Copying Class Objects") will be declared - even if the base class has a constructor and a specific user-created instance. If, in this case, the base class has a custom copy constructor, this base class sub-object is copied using the user-specified copy of ctor (12.8 / 8).

Likewise for the copy assignment operators (12.8 / 10 and 12.8.13).



Thus, you do not need to create custom versions that call the base class unless the derived class needs a custom ctor instance or copy assignment operator for "its own things". However, if a derived class declares and defines its own instance copy operator, ctor / copy, then those custom implementations are responsible for executing correctly against the base class sub-object — this is no longer automatically handled by the compiler.

+4


a source


Only if the derived class has an explicitly defined operator function. Otherwise, the parent class's op function is called. Otherwise, implicit C ++ is called.



0


a source







All Articles