简体   繁体   English

超多重非虚拟继承中基类的范围运算符

[英]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 我只是在搞乱,这不是一个真正的设计

    • so please don't point the problems with the design 所以请不要指出设计中的问题
    • and please don't think this is a good design. 并且请不要认为这是一个好的设计。 It is... not - to say the least 这是...不是-至少可以说
  • This is strictly about the C++ syntax to resolve the ambiguity. 严格来说,这与解决歧义的C ++语法有关。

    • please don't suggest to not do it 请不要建议不要这样做
    • please don't suggest virtual inheritance. 请不要建议虚拟继承。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM