[英]Scope operator for base class in super multiple non-virtual inheritance
Consider this (completely non-nonsensical, but perfectly valid) class inheritance: 考虑一下这个(完全没有道理,但完全有效的)类继承:
struct Area { int size; };
struct Pattern { int size; };
struct R : Area, Pattern {};
struct C : Area, Pattern {};
struct X: R , C {};
Let's see a graph of this great hierarchy: 让我们看一下这个伟大的层次结构图:
Area Pattern
|\ /|
| \/ |
| /\ |
|/ \|
R C
\ /
\/
X
Now, if I am not mistaken, X should have 4 size
members. 现在,如果我没记错的话,X应该有4个size
成员。
How to refer to them using the scope operator? 如何使用范围运算符引用它们?
The obvious solution doesn't work: 显而易见的解决方案不起作用:
X x;
x.R::Area::size = 24;
clang error: 铛错误:
23 : <source>:23:3: error: ambiguous conversion from derived class 'X' to base class 'Area': struct X -> struct R -> struct Area struct X -> struct C -> struct Area xR::Area::size = 8; ^ 1 error generated.
gcc error: gcc错误:
<source>: In function 'auto test()': 23 : <source>:23:14: error: 'Area' is an ambiguous base of 'X' xR::Area::size = 8; ^~~~
Some much needed clarification: 一些急需的澄清:
I was just messing around, it is not a real design 我只是在搞乱,这不是一个真正的设计
This is strictly about the C++ syntax to resolve the ambiguity. 严格来说,这与解决歧义的C ++语法有关。
something like static_cast<R&>(x).Area::size = 8;
像static_cast<R&>(x).Area::size = 8;
which is as ugly as it should be :) 这应该是丑陋的:)
To clarify why the original code doesn't work, it's worth mentioning that a qualified id has the form (among others) type-name::id
so xR::Area::y
is equivalent to using T = R::Area; xT::y;
为了弄清楚为什么原始代码不起作用,值得一提的是,合格的ID具有以下形式: type-name::id
所以xR::Area::y
等于using T = R::Area; xT::y;
using T = R::Area; xT::y;
that clearly does not help as far as disambiguation is concerned. 就消除歧义而言,这显然无济于事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.