虚拟继承中谁调用构造函数?
#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 DV2";}
};
class drv : public dv1, public dv2 {
public:
drv() {std::cout<<"Why is this working";}
};
int main() {
drv obj;
return 0;
}
在虚拟继承的情况下,调用构造函数不是大多数派生类的责任吗? 注意:这里的base是虚拟的、私有的继承。
#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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
drv
的构造函数没有显式调用其基类的构造函数,因此编译器将生成对基类的无参数构造函数的调用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