behaviour of the implicit copy constructor / assignment operator

后端 未结 2 1623
广开言路
广开言路 2021-01-22 03:15

I have a question regarding the C++ Standard.

Suppose you have a base class with user defined copy constructor and assignment operator. The derived class uses the implic

相关标签:
2条回答
  • 2021-01-22 03:44

    If a derived class does not declare a copy constructor, and implicit one will be declared (12.8/4 "Copying class objects") - even if the base class has a user-delcared and defined copy constructor. If the base class has a user-defined copy constructor in this case, that base class sub-object is copied using that user-defined copy ctor (12.8/8).

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

    So you do not necessarily need to implement user defined versions that call the base class if the derived class doesn't need a user-defined copy ctor or copy assignment operator for 'its own stuff'. However, if the derived class does declare and define its own copy ctor/copy assignment operator, then those user-defined implementations are responsible for doing the right thing as far as the base class sub-object is concerned - that is no longer handled by the compiler automatically.

    0 讨论(0)
  • 2021-01-22 03:48

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

    0 讨论(0)
提交回复
热议问题