本文介绍了联盟对当前使用中的成员进行了测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
工会是否具有控制结构来测试当前正在使用哪个成员(或者是否有成员)?我问这个问题是因为未定义的行为永远都不是程序中的好事.
Do unions have a control structure to test which member is currently in use (or if it has any at all)? I'm asking this because undefined behavior is never a good thing to have in your program.
推荐答案
否,现成的机制不存在.您必须自己照顾自己.通常的方法是将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).
这篇关于联盟对当前使用中的成员进行了测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!