class A { ... union { int a; int b; }; }; class B : public A { ... int c; //<- Can this use the same storage as the union? };
一个更具体的例子是标记联合的想法,你希望有一个子类为联合添加一个类型.
I’m wondering if it’s possible to append members to a C++ union in a subclass.
该语言不允许扩展联合.无法将成员附加到工会.
更糟糕的是,与可以通过创建子类(结构)扩展的类和结构不同,联合不能具有基类.它们也可能不被用作基类.