且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

联盟对当前使用中的成员进行了测试

更新时间:2023-12-01 11:01:16

否,现成的机制不存在.您必须自己照顾自己. 通常的方法是将union包装在struct中:

No, no such mechanism exists off-the-shelf. You'll have to take care of that yourself. The usual approach is wrapping the union in a struct:

struct MyUnion
{
   int whichMember;
   union {
      //whatever
   } actualUnion;
};

所以您有MyUnion x;,并且x.whichMember告诉您正在使用x.actualUnion的哪个字段(不过您必须实现功能).

So you have MyUnion x; and x.whichMember tells you which field of x.actualUnion is in use (you have to implement the functionality though).