这里有一个位掩码的选项,如果你实际上不需要使用单个枚举值(例如,你不需要关闭它们)…如果你不担心保持二进制兼容性,即:你不关心你的位在哪里…你可能就是这样。此外,您最好不要过于关注范围和访问控制。嗯,枚举对于位域有一些不错的属性…不知道是否有人尝试过:)
struct AnimalProperties
{
bool HasClaws : 1;
bool CanFly : 1;
bool EatsFish : 1;
bool Endangered : 1;
};
union AnimalDescription
{
AnimalProperties Properties;
int Flags;
};
void TestUnionFlags()
{
AnimalDescription propertiesA;
propertiesA.Properties.CanFly = true;
AnimalDescription propertiesB = propertiesA;
propertiesB.Properties.EatsFish = true;
if( propertiesA.Flags == propertiesB.Flags )
{
cout << "Life is terrible :(";
}
else
{
cout << "Life is great!";
}
AnimalDescription propertiesC = propertiesA;
if( propertiesA.Flags == propertiesC.Flags )
{
cout << "Life is great!";
}
else
{
cout << "Life is terrible :(";
}
}
我们可以看到生命是伟大的,我们有离散的值,我们有一个漂亮的int到&和|到我们的心内容,它仍然有它的位的含义的上下文。一切都是一致的和可预测的……对我来说……只要我继续使用微软的vc++编译器w/ Update 3在Win10 x64上,不碰我的编译器标志:)
Even though everything is great... we have some context as to the meaning of flags now, since its in a union w/ the bitfield in the terrible real world where your program may be be responsible for more than a single discrete task you could still accidentally (quite easily) smash two flags fields of different unions together (say, AnimalProperties and ObjectProperties, since they're both ints), mixing up all yours bits, which is a horrible bug to trace down... and how I know many people on this post don't work with bitmasks very often, since building them is easy and maintaining them is hard.
class AnimalDefinition {
public:
static AnimalDefinition *GetAnimalDefinition( AnimalFlags flags ); //A little too obvious for my taste... NEXT!
static AnimalDefinition *GetAnimalDefinition( AnimalProperties properties ); //Oh I see how to use this! BORING, NEXT!
static AnimalDefinition *GetAnimalDefinition( int flags ); //hmm, wish I could see how to construct a valid "flags" int without CrossFingers+Ctrl+Shift+F("Animal*"). Maybe just hard-code 16 or something?
AnimalFlags animalFlags; //Well this is *way* too hard to break unintentionally, screw this!
int flags; //PERFECT! Nothing will ever go wrong here...
//wait, what values are used for this particular flags field? Is this AnimalFlags or ObjectFlags? Or is it RuntimePlatformFlags? Does it matter? Where's the documentation?
//Well luckily anyone in the code base and get confused and destroy the whole program! At least I don't need to static_cast anymore, phew!
private:
AnimalDescription m_description; //Oh I know what this is. All of the mystery and excitement of life has been stolen away :(
}
因此,然后你让你的联合声明私有,以防止直接访问“Flags”,并必须添加getter /setter和操作符重载,然后为所有这些做一个宏,你基本上回到你开始的地方,当你试图用Enum来做这件事。
不幸的是,如果你想要你的代码是可移植的,我不认为有任何方法可以A)保证位布局或B)在编译时确定位布局(这样你就可以跟踪它,至少纠正跨版本/平台等的变化)
带位字段的结构中的偏移量
在运行时,你可以玩一些技巧,设置字段和XORing标志,看看哪些位发生了变化,听起来对我来说很糟糕,尽管有一个100%一致的,平台独立的,完全确定的解决方案,即:ENUM。
TL;DR:
Don't listen to the haters. C++ is not English. Just because the literal definition of an abbreviated keyword inherited from C might not fit your usage doesn't mean you shouldn't use it when the C and C++ definition of the keyword absolutely includes your use case. You can also use structs to model things other than structures, and classes for things other than school and social caste. You may use float for values which are grounded. You may use char for variables which are neither un-burnt nor a person in a novel, play, or movie. Any programmer who goes to the dictionary to determine the meaning of a keyword before the language spec is a... well I'll hold my tongue there.
如果你确实希望你的代码模仿口语,你最好使用Objective-C编写,顺便说一句,它也大量使用枚举作为位域。