Copy constructor is not inherited

后端 未结 3 862
感动是毒
感动是毒 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:57

    For a moment, we’ll assume ‘copy constructor inheritance’ is allowed. Having your class structure intact, please consider following code for modified main method.

    int main() {
        C c;
        D d;
        D d_from_d(d);
        D d_from_c(c); // does not compile, copy ctor is not inherited
        D d_from_int(1); // compiles, C(int) is inherited
    }  
    

    In D d_from_d(d), as a normal constructor call, there will be two copy constructor calls. One for C::C(const C&) and the other one is for compiler generated copy constructor for D. Having source object type in D (d in this case), C’s copy constructor can copy d’s C attributes while compiler generated D’s copy constructor can copy d’s D attribute.

    But in D d_from_c(c) case, There is no problem for C’s copy constructor because, c’s C attributes can be copies by C’s copy constructor. But how does the compiler generated D’s copy constructor know the way to copy ‘D’s attributes from C’s object’. This is a conflict which should be avoided.

    But, if you provide some sort of ‘weird copy constructor’ (you may need to a default constructor as well) like;

    D(const C & c):C(c){} 
    

    Then, calling D d_from_c(c); is valid. Because, now we have explicitly provided a matching ‘copy’ constructor.

    So, saying ‘Inheriting copy constructors are now allowed’ is invalid.

提交回复
热议问题