Assignment operator inheritance

前端 未结 6 1096
[愿得一人]
[愿得一人] 2020-11-29 04:09

There is this code:

#include 

class Base {
public:
    Base(){
        std::cout << \"Constructor base\" << std::endl;
    }
            


        
6条回答
  •  -上瘾入骨i
    2020-11-29 04:53

    Standard says (12.8):

    An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (12.8), a base class assignment operator is always hidden by the copy assignment operator of the derived class.

    and then assignment operator of derived call your base

    The implicitly-defined copy/move assignment operator for a non-union class X performs memberwise copy-/move assignment of its subobjects. The direct base classes of X are assigned first, in the order of their declaration in the base-specifier-list, and then the immediate non-static data members of X are assigned, in the order in which they were declared in the class definition.

提交回复
热议问题