如何在c ++中动态创建联合实例?

der*_*dji 4 c++ instance unions

我需要将union的几个实例作为类变量,所以我如何在堆中创建一个union实例?谢谢

RC.*_*RC. 8

与创建任何其他对象相同:

union MyUnion
{
   unsigned char charValue[5];
   unsigned int  intValue;
};


MyUnion *myUnion = new MyUnion;
Run Code Online (Sandbox Code Playgroud)

你的工会现在已经堆满了.请注意,union是其最大数据成员的大小.