开发者

Who calls constructor in virtual inheritance? [duplicate]

开发者 https://www.devze.com 2023-02-02 07:03 出处:网络
This question already has answers here: c++ virtual inheritance (3 answers) Closed 9 years ago. #include<iostream>
This question already has answers here: c++ virtual inheritance (3 answers) Closed 9 years ago.
#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

0

精彩评论

暂无评论...
验证码 换一张
取 消