Who calls constructor in virtual inheritance? [duplicate]
#include<iostream>
class base{
public:
base(){std::cout<<"In base";}
};
class dv1:virtual private base {
public:
dv1(){std::cout<<"In DV1";}
};
class dv2:virtual private base {
public:
dv2(){std::cout<<"In 开发者_JAVA百科DV2";}
};
class drv : public dv1, public dv2 {
public:
drv() {std::cout<<"Why is this working";}
};
int main() {
drv obj;
return 0;
}
Isn't in case of virtual inheritance, it is the responsibility of most derive class to call the constructor? Note: Here base is inherited virtually and privately.
Your constructor of drv
didn't explicitly call the constructors of its base class(es), so the compiler will generate a call to the parameterless constructor of the base class
精彩评论