具有混合继承修饰符的钻石继承(受保护/私有/公共)
假设我们有类 A,B,C,D
,其中 A 是基础,B,C 是介于两者之间,D 是在钻石模型中派生的。
注意:
B 类
在私有模式下虚拟继承A 类
,
C 类
继承保护模式下的 virtualy A 类
。
class A
{
public:
int member; // note this member
};
class B :
virtual private A // note private
{
};
class C :
virtual protected A // note protected
{
};
class D :
public B, // doesn't metter public or whatever here
public C
{
};
int main()
{
D test;
test.member = 0; // WHAT IS member? protected or private member?
cin.ignore();
return 0;
}
现在,当我们创建class D
的实例时,成员会是什么?私人的还是受保护的哈哈?
图 2:
如果我们这样做会怎样:
class B :
virtual public A // note public this time!
{
};
class C :
virtual protected A // same as before
{
};
我想 member
在第二个示例中将是公开的,不是吗?
let's say we have class A,B,C,D
where A is base, B,C are between and D is derived in diamond model.
NOTE:
class B
inherits virtualy class A
in private mode,
class C
inherita virtualy class A
in protected mode.
class A
{
public:
int member; // note this member
};
class B :
virtual private A // note private
{
};
class C :
virtual protected A // note protected
{
};
class D :
public B, // doesn't metter public or whatever here
public C
{
};
int main()
{
D test;
test.member = 0; // WHAT IS member? protected or private member?
cin.ignore();
return 0;
}
now when we make an instance of class D
what will member be then? private or protected lol?
Figure No2:
what if we make it so:
class B :
virtual public A // note public this time!
{
};
class C :
virtual protected A // same as before
{
};
I suppose member
will be public in this second example isn it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
§11.6 多路访问 [class.paths]
我认为我不需要添加任何其他内容,但另请参阅此缺陷报告(已关闭为“不是缺陷”)。
§11.6 Multiple access [class.paths]
I think I don't need to add anything else, but see also this defect report (which was closed as "not a defect").