虚拟地狱 [英] virtual hell

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

问题描述

我相信人们熟悉通过

虚拟继承解决的脏钻石问题。看看下面的代码并告诉我有多少个基类(在这种情况下为A)的
实例会接收D类?

A类

{






};

B类:虚拟公共A {







};

class C:虚拟公共A {$ / b> b:




};

D类:公共B ,公共C,虚拟公众A {







};

====


一个小的跟进:默认是公共继承,受保护或

私有这样的A类:B {};?

有什么区别

A级

{






};

B级:虚拟公共A {







};

D级:公共B {







};




D类:虚拟公共B {






};

============

下一篇:你可以内联虚函数吗?行为未定义,

非标准等等?

I am sure people are familiar with dirty diamond problem solved by
virtual inheritance. look at the code below and tell me how many
instances of the base class (in this case A) does class D receive?
class A
{
:
:

};
class B: virtual public A {

:
:

};
class C: virtual public A {

:
:

};
class D: public B, public C, virtual public A {

:
:

};
====

A small follow up: is inheritance by default public, protected or
private such class A: B{};?
what is the difference between
class A
{
:
:

};
class B: virtual public A {

:
:

};
class D:public B {

:
:

};
OR

class D: virtual public B {
:
:

};
============
Next: can you inline the virtual functions? is the behavior undefined,
nonstandard, etc?

推荐答案

puzzlecracker写道:
puzzlecracker wrote:
我相信人们熟悉通过虚拟继承解决的脏钻石问题。看看下面的代码并告诉我D类接收多少个基类实例(在本例中为A)?

class A
{
} ; B / B类:虚拟公众A {
};

C类:虚拟公众A {
};
D类:公共B,公共C,虚拟公众A {
};


一个。所有派生自A的实例都是虚拟的。


BTW,你真的需要保存空白。如果我们像你在帖子中那样浪费

这样的空白,我们可能会比我们想象的更早耗尽它们。

== ==

一个小的跟进:默认是继承公共,受保护或
私人这样的类A:B {};?


默认为类别私有,结构为公共。


A类之间有什么区别
{
};

B类:虚拟公众A {
};

D类:公共B {
};



AND

D类:虚拟公共B {
};


没有区别。在这两种情况下,'A''是一个虚拟基类,并且

必须在''D'中构建。在这两种情况下,''B''是直接基类

''D''并且必须在''D''中构建。

=== =========
下一篇:您可以内联虚拟功能吗?是不定义的行为,
非标准等?
I am sure people are familiar with dirty diamond problem solved by
virtual inheritance. look at the code below and tell me how many
instances of the base class (in this case A) does class D receive?
class A
{
};

class B: virtual public A {
};

class C: virtual public A {
};

class D: public B, public C, virtual public A {
};
One. All instances of deriving from ''A'' are virtual.

BTW, you really need to conserve whitespace. If we go about wasting
whitespace like you did in your post, we may run out of it sooner than
we think.
====

A small follow up: is inheritance by default public, protected or
private such class A: B{};?
Default is private for classes, public for structs.
what is the difference between
class A
{
};

class B: virtual public A {
};

class D:public B {
};

OR
AND
class D: virtual public B {
};
There is no difference. In both cases ''A'' is a virtual base class and
has to be constructed in ''D''. In both cases ''B'' is a direct base class
of ''D'' and has to be constructed in ''D''.
============
Next: can you inline the virtual functions? is the behavior undefined,
nonstandard, etc?




我可以。行为很好。


V



I can. The behaviour is just fine.

V


非常感谢Victor。


只是一个小小的:
Thanks so much Victor.

just a little small one:
一个。所有派生自''A'的实例都是虚拟的
One. All instances of deriving from ''A'' are virtual




但是如何在子类中只存在一个实例

当推导是虚拟的时候......我只是可以看到

发生在幕后的过程......任何想法?



but how is it done that that only one instance exists in the subclass
when the derivation is virtual... I just can visualize the process that
takes place behind the scene... any ideas?


非常感谢Victor。


只是一个小小的:
Thanks so much Victor.

just a little small one:
一。所有派生自''A'的实例都是虚拟的
One. All instances of deriving from ''A'' are virtual




但是如何在子类中只存在一个实例

当推导是虚拟的时候......我只是可以看到

发生在幕后的过程......任何想法?



but how is it done that that only one instance exists in the subclass
when the derivation is virtual... I just can visualize the process that
takes place behind the scene... any ideas?


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

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