为什么我不应该在标志枚举中使用0 [英] Why should I never use 0 in a flag enum

查看:99
本文介绍了为什么我不应该在标志枚举中使用0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

枚举应以0还是1开头?

为什么我不应该在标志枚举中使用0?我已经读过多次了,想让
知道原因:)

Why should I never use 0 in a flag enum? I have read this multiple times now and would like to know the reason :)

推荐答案


为什么我不应该在标志枚举中使用0?

Why should I never use 0 in a flag enum?

该问题是基于错误的假设提出的。您应该始终在标志枚举中使用零。应始终将其设置为无。

The question is predicated on an incorrect assumption. You should always use zero in a flag enum. It should always be set to mean "None".

除了表示未设置任何标志外,请勿将其用于其他任何用途。

You should never use it for anything other than to represent "none of the flags are set".


为什么不呢?

Why not?

因为如果零的含义不是 none,那会很令人困惑。人们有一个合理的期望,即((e& E.X)== E.X)表示是否设置了X标志?但是如果X为零,那么即使逻辑上标记未设置,该表达式也将始终为true。

Because it gets really confusing if zero has a meaning other than "none". One has the reasonable expectation that ((e & E.X) == E.X) means "Is the X flag set?" but if X is zero then this expression will always be true, even if logically the flag is not "set".

这篇关于为什么我不应该在标志枚举中使用0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆