问题描述
我有两个不同的枚举定义,同名成员。
编译器抱怨重复定义。这是预期的
行为吗?
我不能在不同的枚举定义中使用相同名称的字段吗?我认为
它应该完全有效。它是一个愚蠢的限制
必须定义不相交的枚举符号定义。这就像在结构中拥有不相交的成员名字一样
。
Bahadir
Hi,
I have two different enum definitions with members of same name. The
compiler complains about duplicate definitions. Is this expected
behaviour?
Can''t I have same-named fields in different enum definitions? I think
it should have been perfectly valid to do that. Its a stupid limitation
to have to define disjoint enum symbol definitions. This is like having
to have disjoint member names in structures.
Bahadir
推荐答案
是的,枚举常量在一个相同的命名空间中(它们与函数共享
) ,变量和typedef名称)。
见FAQ中的1.29。
Yes, enumeration constants are in one and the same namespace (which
they share with functions, variables and typedef names).
See 1.29 from the FAQ.
不是真的。如果会员名字相同,你无法知道哪个enum
a会员所属。
enum forward {ZERO,ONE};
enum落后{ONE,ZERO};
返回ZERO;
或许,
如果你的枚举成员在同一个值上有相同的名字,
你只用一个枚举就可以做得更好吗?
-
pete
Not really. You have no way of knowing which enum
a member belongs to, if members have the same name.
enum forward {ZERO, ONE};
enum backward{ONE, ZERO};
return ZERO;
Perhaps,
if your enum members have the same name at the same value,
you could do better with only one enum?
--
pete
你总是可以切换到C ++。它具有可以做你想要的
的功能。除此之外,标准做法是为每个枚举类型使用
唯一前缀。
-
Ben Pfaff
电子邮件:
web:
这篇关于枚举成员命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!