帮助:虚拟继承,非默认构造函数 [英] Help: virtual inheritance, non-default constructor

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

问题描述

编译以下程序时,我在指定的行上出错:

基类B无法初始化,因为它没有默认的

构造函数。


如果我删除了虚拟继承B(这样它使用普通的

继承),它编译得很好。如果我明确地为B调用

构造函数,它也会编译,如错误后的注释中所示。我是

不知道任何理由认为虚拟继承在这个

方面应该是特殊的。怎么了?


B级

{

公开:

B(int ){}

};


C类:虚拟公共B

{

public:

C():B(1){}

};


D级:公共C

{

public:

D(){} //这里错误

// D():B(1){ } - 但是编译好了

};


int main()

{

返回0;

}

When compiling the following program, I get an error on the line specified:
The base class "B" cannot be initialized because it does not have a default
constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error. I''m
not aware of any reason that virtual inheritance should be special in this
respect. What''s wrong?

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

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

class D : public C
{
public:
D(){} // error here
// D():B(1){} - this however compiles OK
};

int main()
{
return 0;
}

推荐答案



" jeffc" <无**** @ nowhere.com>在消息新闻中写道:3f ******** @ news1.prserv.net ...

"jeffc" <no****@nowhere.com> wrote in message news:3f********@news1.prserv.net...
编译以下程序时,我在指定的行上出错:
基类B和无法初始化,因为它没有默认的
构造函数。
When compiling the following program, I get an error on the line specified:
The base class "B" cannot be initialized because it does not have a default
constructor.



虚拟基础的初始化由最派生的对象完成。

你的D类必须为虚拟基础提供必要的初始化程序

B.虚拟基础只在初始化任何非虚拟的
基础之前初始化。


The initialization of a virtual base is done by the most derived object.
Your class D must provide the necessary initializers for the virtual base
B. The virtual base is initialized just once and before any of the non-virtual
bases are initialized.




" jeffc" <无**** @ nowhere.com>写在消息

新闻:3f ******** @ news1.prserv.net ...

"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
编译以下程序时,我收到错误消息指定的行
:基类B;无法初始化,因为它没有
默认构造函数。

如果我删除虚拟构造函数。 B的继承(这样它使用普通的继承),它编译得很好。如果我显式调用B的
构造函数,它也会编译,如错误后的注释中所示。
我不知道任何理由认为虚拟继承在这个方面应该是特殊的。有什么问题?
When compiling the following program, I get an error on the line specified: The base class "B" cannot be initialized because it does not have a default constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error. I''m not aware of any reason that virtual inheritance should be special in this
respect. What''s wrong?




我终于在Effective C ++中找到了对这个问题的引用。 Meyers

声明参数在成员初始化列表中指定

类*最基本来自* *。因此,初始化虚拟基础的类可能在

继承图中任意远离它,此外,执行初始化的类

可以随着新类添加到层次结构中而改变。


所以,3个问题:

1)为什么会这样?

2)他暗示一旦将新类添加到层次结构中,必须在那里执行

初始化*而不是基类中的*?

(因为这似乎与我发布的代码中发生的情况不一致 -

派生类和基类都初始化虚拟基础。

3)为什么这样完全没有任何区别,因为我甚至没有使用

多重继承?它只是一个编译器实现问题吗?



I finally found a reference to this problem in "Effective C++". Meyers
states that "arguments are specified in the member initialization lists of
the classes *most derived* from the base. As a result, the class
initializing a virtual base may be arbitrarily far from it in the
inheritance graph, and furthermore, the class performing the initialization
can change as new classes are added to the hierarchy."

So, 3 questions:
1) why is this so?
2) is he implying that once a new class is added to the hierarchy, the
initialization must be performed there *instead of* in the base class?
(because this seems inconsistent with what happens in the code I posted -
both the derived class and base class initialize the virtual base.
3) why does this make any difference at all, since I''m not even using
multiple inheritance? Is it merely a compiler implementation issue?


jeffc写道:
jeffc wrote:
" jeffc" <无**** @ nowhere.com>在消息中写道
新闻:3f ******** @ news1.prserv.net ...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
编译以下程序时,出现错误指定的行
When compiling the following program, I get an error on the line





specified:

基类B无法初始化,因为它没有
The base class "B" cannot be initialized because it does not have a


默认



default

构造函数。

如果我删除了虚拟" B的继承(这样它使用普通的继承),它编译得很好。如果我明确地调用B的
构造函数,它也会编译,如错误后的注释所示。
constructor.

If I remove the "virtual" inheritance of B (so that it uses plain
inheritance), it compiles fine. It also compiles if I invoke the
constructor for B explicitly, as shown in the comment after the error.



我是



I''m

不知道虚拟继承在这个方面应该特别的任何原因。有什么问题?
not aware of any reason that virtual inheritance should be special in this
respect. What''s wrong?



我终于在Effective C ++中找到了对这个问题的引用。 Meyers
声明参数是在成员初始化列表中指定的类* *最基础的*。因此,在
继承图中,初始化虚拟基础的类可能与它任意相距很远,而且,执行初始化的类可以随着新类添加到层次结构。

所以,3个问题:
1)为什么会这样?
2)他暗示一旦新的类被添加到层次结构中,<必须在那里执行初始化*而不是基类中的*?
(因为这似乎与我发布的代码中发生的事情不一致 -
派生类和基类都初始化虚拟基础。
3)为什么这会产生任何不同,因为我甚至没有使用
多重继承?它仅仅是一个编译器实现问题吗?


I finally found a reference to this problem in "Effective C++". Meyers
states that "arguments are specified in the member initialization lists of
the classes *most derived* from the base. As a result, the class
initializing a virtual base may be arbitrarily far from it in the
inheritance graph, and furthermore, the class performing the initialization
can change as new classes are added to the hierarchy."

So, 3 questions:
1) why is this so?
2) is he implying that once a new class is added to the hierarchy, the
initialization must be performed there *instead of* in the base class?
(because this seems inconsistent with what happens in the code I posted -
both the derived class and base class initialize the virtual base.
3) why does this make any difference at all, since I''m not even using
multiple inheritance? Is it merely a compiler implementation issue?




IIRC在我的Meyer'的副本中,他的开头是Just you you

以为你明白了,他们改变了规则!或者一些

这样的。一页以后我相信他会解释原因。



IIRC in my copy of Meyer''s he starts of with "Just when you
thought you understood it, they change the rules!" or some
such. A page onwards I believe he gives an explaination of why.


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

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