虚拟继承问题 [英] virtual inheritance question

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

问题描述

大家好,


i我的遗产问题。

考虑以下因素:


class A

{

public:

A(int i){;}

};


B级:虚拟公共A

{

public:

B(int i):A(i ){;}

};


C类:虚拟公共B

{

public :

C(int i):B(i){;} //引发错误C2512:''A :: A'':不合适

默认构造函数可用

};


有人能告诉我为什么会收到这个错误吗?它与B

具有来自A的虚拟继承这一事实有关。如果我使其成为非虚拟的,则错误是
消失了。在这种情况下,虚拟关键字的影响是什么?


亲切的问候,

布鲁诺。

解决方案

Bruno van Dooren写道:

大家好,

我的继承问题。
A级
{
公开:
A(int i){;}
};

B级:虚拟公众A
{
公开:
B(int i):A(i){;}
};
C类:虚拟公共B
公开:
C(int i):B(i){;} //引发错误C2512:''A :: A'':没有合适的
默认构造函数
};

有人能告诉我为什么会出现这个错误吗?它与B
具有来自A的虚拟继承这一事实有关。如果我将其设为非虚拟,则错误消失了。在这种情况下,''virtual''关键字的影响是什么?




当你使用虚拟继承时,你需要初始化每个虚拟的
$ b在最派生的类构造函数中$ b基类,因为如果不是这样的话,你可能会有多个基数进行初始化

(例如经典菱形虚拟继承示例)。所以你的C类

构造函数需要初始化A基础对象以及B基础 -

B'的初始化A在创建C对象时被忽略。


Tom


> (例如经典的钻石虚拟继承示例)。因此,你的C类

构造函数需要初始化A基础对象以及B基础对象 - 在创建C对象时,将忽略B的A初始化。

汤姆




谢谢。然而,这有以下含义:

请考虑一个新的场景:


A级

{

public:

A(int i){;}

};


B类:虚拟公共A

{

public:

B(int i):A(i){;}

};


C类:虚拟公共A

{

public:

C(int i): A(i){;}

};


D类:虚拟公共B,C

{

public:

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

};


在这种情况下我使用相同的A实例结束了C和B.

多重继承是唯一可以看到用于
虚拟继承。

这样看着它,似乎在继承上下文中唯一使用''virtual''关键字

是说继承我的人必须初始化

我的基类太多了


这个在
成员函数的上下文中,含义似乎与虚拟的含义无关。这是正确的吗?


亲切的问候,

布鲁诺。


Bruno van Dooren写道:< blockquote class =post_quotes>

(例如经典钻石虚拟继承示例)。所以你的C类
构造函数需要初始化A基础对象以及B对象 - 在创建C对象时,B'的A初始化被忽略。

汤姆

谢谢。然而,这具有以下意义:
请考虑一个新的场景:

A级
公开:
A(int i){; }
};

B类:虚拟公众A
公开:
B(int i):A(i){;} < c:虚拟公众A
{
公开:
C(int i):A(i){;} };

D类:虚拟公共B,C
{
公开:
D(int i):C(i),B(i), A(i){;}
};

在这种情况下,我最终使用相同的A实例进行C和B.




....当它们是D的子对象时。那是我提到的经典钻石

继承示例。当你创建一个D对象时,只使用了A的初始化。

多重继承是唯一可以看到用于
看起来像这样,似乎在继承上下文中唯一使用''虚拟''关键字
就是说谁从我这里继承了初始化
我的基类也是


对于单继承,是 - 虚拟继承实际上适用于多个继承已经或将来可能使用的情况。



标准库将它用于isosream和ostream的ios基类,

以确保iostream只有一个ios子对象(basic_前缀

为了清楚起见而已。)

这个含义似乎与成员函数的上下文中虚拟的含义无关。这是正确的吗?




是的。


Tom


Hi all,

i am having a problems with inheritance.
consider the following:

class A
{
public:
A(int i){;}
};

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

class C: virtual public B
{
public:
C(int i) : B(i){;} //raises error C2512: ''A::A'' : no appropriate
default constructor available
};

can someone tell me why i get that error? it has to do with the fact that B
has virtual inheritance from A. if i make that non-virtual, the error is
gone. what is the impact of the ''virtual'' keyword in this situation?

kind regards,
Bruno.

解决方案

Bruno van Dooren wrote:

Hi all,

i am having a problems with inheritance.
consider the following:

class A
{
public:
A(int i){;}
};

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

class C: virtual public B
{
public:
C(int i) : B(i){;} //raises error C2512: ''A::A'' : no appropriate
default constructor available
};

can someone tell me why i get that error? it has to do with the fact that B
has virtual inheritance from A. if i make that non-virtual, the error is
gone. what is the impact of the ''virtual'' keyword in this situation?



When you use virtual inheritence, you need to initialize each virtual
base class in the most derived class constructor, since you might have
multiple bases that would do the initialisation if this wasn''t the case
(e.g. the classic diamond virtual inheritance example). So your C class
constructor needs to initialize the A base object as well as the B one -
B''s initialization of A is ignored when creating a C object.

Tom


> (e.g. the classic diamond virtual inheritance example). So your C class

constructor needs to initialize the A base object as well as the B one -
B''s initialization of A is ignored when creating a C object.

Tom



thanks. however, this has the followig implications:
please consider a new scenario:

class A
{
public:
A(int i){;}
};

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

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

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

in this case i end up with C and B using the same instance of A.
multiple inheritance is the only scenario in which i can see a use for
virtual inheritance.
looking at it like this, it seems that the only use of the ''virtual'' keyword
in inheritance context is to say "whoever inherits from me has to initialize
my base class too"

this meaning seems unrelated to the meaning of virtual in the context of
member functions. is that correct?

kind regards,
Bruno.


Bruno van Dooren wrote:

(e.g. the classic diamond virtual inheritance example). So your C class
constructor needs to initialize the A base object as well as the B one -
B''s initialization of A is ignored when creating a C object.

Tom

thanks. however, this has the followig implications:
please consider a new scenario:

class A
{
public:
A(int i){;}
};

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

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

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

in this case i end up with C and B using the same instance of A.



.... when they are subobjects of a D. That''s the classic diamond
inheritance example I mentioned. When you create a D object, only D''s
initialization of A is used.
multiple inheritance is the only scenario in which i can see a use for
virtual inheritance.

looking at it like this, it seems that the only use of the ''virtual'' keyword
in inheritance context is to say "whoever inherits from me has to initialize
my base class too"
For single inheritance, yes - virtual inheritance is really for cases
where multiple inheritance is, or may be in the future, employed. The
standard library uses it for the ios base class of istream and ostream,
to ensure that iostream only has one ios subobject (basic_ prefixes
elided for clarity).
this meaning seems unrelated to the meaning of virtual in the context of
member functions. is that correct?



Yes.

Tom


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

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