覆盖超类联合的子类联合ontop

Mat*_* G. 7 c++ inheritance unions

我想知道是否可以将成员附加到子类中的C++联合.

class A { 
    ...
    union { int a; int b; };
 };

 class B : public A {
     ...
     int c; //<- Can this use the same storage as the union?
 };
Run Code Online (Sandbox Code Playgroud)

一个更具体的例子是标记联合的想法,你希望有一个子类为联合添加一个类型.

R S*_*ahu 4

你说,

我想知道是否可以将成员附加到子类中的 C++ 联合。

该语言不允许扩展union. union无法将成员追加到 a 中。

更糟糕的是,与classes 和structs 不同,它们可以通过创建子类(结构体)来扩展,unions 不能有基类。它们也不能用作基类。