更新时间: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).