C ++私有虚拟继承问题 [英] C++ private virtual inheritance problem

查看:140
本文介绍了C ++私有虚拟继承问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,似乎C类不能访问A的构造函数,这是必需的,因为虚拟继承。然而,代码仍然编译和运行。为什么它可以工作?

In the following code, it seems class C does not have access to A's constructor, which is required because of the virtual inheritance. Yet, the code still compiles and runs. Why does it work?

class A {};
class B: private virtual A {};
class C: public B {};

int main() {
    C c;
    return 0;
}



<

Moreover, if I remove the default constructor from A, e.g.

class A {
public:
    A(int) {}
};
class B: private virtual A {
public:
    B() : A(3) {}
};

然后

class C: public B {};

会(意外)编译,但

class C: public B {
public:
    C() {}
};

无法正常编译。

使用g ++(GCC)3.4.4(cygming special,gdc 0.12,使用dmd 0.125)编译的代码,但已经验证其与其他编译器的行为相同。

Code compiled with "g++ (GCC) 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)", but it has been verified to behave the same with other compilers as well.

推荐答案

根据 C ++无法从中衍生出具有虚拟专用库的Core Issue#7 类。这是编译器中的错误。

According to C++ Core Issue #7 class with a virtual private base can't be derived from. This is a bug in compiler.

这篇关于C ++私有虚拟继承问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆