枚举成员命名空间冲突 [英] enum members namespace clash

查看:117
本文介绍了枚举成员命名空间冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有两个不同的枚举定义,同名成员。

编译器抱怨重复定义。这是预期的

行为吗?


我不能在不同的枚举定义中使用相同名称的字段吗?我认为

它应该完全有效。它是一个愚蠢的限制

必须定义不相交的枚举符号定义。这就像在结构中拥有不相交的成员名字一样




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

推荐答案

2005年6月2日09:12:04 -0700, Bi ** ***********@gmail.com 写道:
On 2 Jun 2005 09:12:04 -0700, Bi*************@gmail.com wrote:


我有两个不同的枚举定义同名成员。
编译器抱怨重复的定义。这是预期的行为吗?
Hi,

I have two different enum definitions with members of same name. The
compiler complains about duplicate definitions. Is this expected
behaviour?




是的,枚举常量在一个相同的命名空间中(它们与函数共享
) ,变量和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.


Bi ************* @ gmail.com 写道:
<

我有两个不同的枚举定义,同名成员。
编译器抱怨重复的定义。这是预期的行为吗?

我不能在不同的枚举定义中使用同名的字段吗?我认为
它应该是完全有效的。
这是一个愚蠢的限制,必须定义不相交的枚举符号定义。
这就像是
在结构中有不相交的成员名称。

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.




不是真的。如果会员名字相同,你无法知道哪个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


Bi **** *********@gmail.com 写道:
Bi*************@gmail.com writes:
我有两个不同的枚举定义,同名成员。
编译器抱怨重复的定义。这是预期的行为吗?


是的。

我不能在不同的枚举定义中使用相同名称的字段吗?


No.

我认为应该完全有效。它是一个愚蠢的限制,必须定义不相交的枚举符号
定义。这就像在结构中必须有不相交的成员名称。
I have two different enum definitions with members of same name. The
compiler complains about duplicate definitions. Is this expected
behaviour?
Yes.
Can''t I have same-named fields in different enum definitions?
No.
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.




你总是可以切换到C ++。它具有可以做你想要的
的功能。除此之外,标准做法是为每个枚举类型使用

唯一前缀。

-

Ben Pfaff

电子邮件: bl*@cs.stanford.edu

web: http://benpfaff.org


这篇关于枚举成员命名空间冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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