Copy constructor is not inherited

后端 未结 3 847
感动是毒
感动是毒 2020-11-28 12:16

I\'ve got the following code:

class C {
public:
    C(int) {}
    C(const C&) {}
    C() {}
};  

class D : public C { 
public:
    using C::C;
};  

int         


        
3条回答
  •  清酒与你
    2020-11-28 12:39

    Derived class should inherit all ctors of base except the default ctor

    No, that's not true, see T.C.'s answer for the real rule.

    The purpose of inheriting constructors is to say "the derived type can be created from the same arguments as the base type", but that isn't relevant for the base class' copy constructor, because a copy constructor is not just a way of saying how to create a type from a given argument.

    A copy constructor is special, it's for copying an object of the same type.

    A constructor D(const C&) would not used be for copying an object of the same type, because C is not the same type as D.

提交回复
热议问题