默认构造函数和虚拟继承 [英] Default constructor and virtual inheritance

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

问题描述

对于没有提供默认构造函数的类,可以有虚拟继承吗?

Is possible to have virtual inheritance for class not providing default constructor?

本菱形图(最简单的一个没有提供默认构造函数)不编译(g ++ 4.4.3)。

The present diamond diagram (the simplest one with the only change of no default constructor provided) does not compile (g++ 4.4.3).

class A {
 public: 
  A(int ) {}
};
class B : virtual public A {
 public:
  B(int i) : A(i) {}
};
class C : virtual public A {
 public:
  C(int i) : A(i) {}
};
class D : public B, public C {
 public:
  D(int i) : B(i), C(i) {}
};

感谢,
Francesco

Thanks, Francesco

推荐答案

您需要在这里明确调用 A 的构造函数

You need to call A's constructor explicitly here

 D(int i) : A(i), B(i), C(i) {}




虚拟基类是特殊的,因为它们是由最大派生类初始化的,而不是从虚拟基类继承的任何中间基类。哪个潜在的多个初始化器将正确选择初始化一个基础?

virtual base classes are special in that they are initialized by the most derived class and not by any intermediate base classes that inherits from the virtual base. Which of the potential multiple initializers would the correct choice for initializing the one base?

如果构造的最大派生类没有在它的成员初始化列表中列出,

If the most derived class being constructed does not list it in its member initalization list then the virtual base class is initialized with its default constructor which must exist and be accessible.

无耻地从在这里: - )

这篇关于默认构造函数和虚拟继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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