超多重非虚继承中基类的作用域运算符 [英] Scope operator for base class in super multiple non-virtual inheritance

查看:59
本文介绍了超多重非虚继承中基类的作用域运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个(完全没有意义,但完全有效)类继承:

struct Area { int size;};结构模式 { 整数大小;};结构 R :区域,模式{};结构 C:区域,模式 {};结构 X: R , C {};

让我们看看这个伟大层次结构的图表:

区域模式|\/||\/||/\ ||/\|电阻率\/\/X

现在,如果我没记错的话,X 应该有 4 个 size 成员.

如何使用作用域运算符来引用它们?

显而易见的解决方案不起作用:

X x;x.R::Area::size = 24;

clang 错误:

<块引用>

23 : :23:3: 错误:从派生类X"到基类Area"的不明确转换:结构 X ->结构 R ->结构区结构 X ->结构 C ->结构区x.R::Area::size = 8;^产生了 1 个错误.

gcc 错误:

<块引用>

:在函数auto test()"中:23 : <source>:23:14: 错误: 'Area' 是'X' 的不明确基数x.R::Area::size = 8;^~~~

<小时>

一些急需的澄清:

  • 我只是在胡闹,这不是真正的设计

    • 所以请不要指出设计的问题
    • 请不要认为这是一个好的设计.这是...不是 - 至少可以说
  • 这完全是关于解决歧义的 C++ 语法.

    • 请不要建议不要这样做
    • 请不要建议虚拟继承.

解决方案

类似于 static_cast(x).Area::size = 8;

它应该是丑陋的:)

为了阐明为什么原始代码不起作用,值得一提的是,合格的 id 具有以下形式(除其他外)type-name::id 所以 xR::Area::y 等价于 使用 T = R::Area;x.T::y; 就消除歧义而言,这显然无济于事.

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.

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
  x.R::Area::size = 8;
  ^
1 error generated.

gcc error:

<source>: In function 'auto test()':
23 : <source>:23:14: error: 'Area' is an ambiguous base of 'X'
   x.R::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.

    • please don't suggest to not do it
    • please don't suggest virtual inheritance.

解决方案

something like 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 x.R::Area::y is equivalent to using T = R::Area; x.T::y; that clearly does not help as far as disambiguation is concerned.

这篇关于超多重非虚继承中基类的作用域运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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