枚举 [英] Enums
问题描述
我正在宣布一个枚举,我想限制范围
我知道
enum day2 {sun, mon,};
相当于
static const int sun = 0;
static const int mon = 1;
但我不想让这些全球化。
我可以做day2 :: sun并使用它但是关于非标准扩展,我收到一个警告
。它确实有效。
我想我可以把它包装在一个静态类中并使用它......也许类似于
static struct day2
{
static const int sun = 0;
static const int mon = 1;
}
这似乎有效,但我在第2天遇到没有变量的错误。我想我
可以通过某种方式解决这个问题,但我想知道是否有其他更好的
方法。 (每次使用静态常量int有点麻烦,如果它不需要
)
有什么想法吗?
谢谢,
Jon
Actualy我正在做的是
class X
{
enum Y
{
};
};
似乎工作正常。
Jon Slaughter写道:
我正在宣布一个枚举,我想限制范围
我知道
enum day2 {sun,mon,};
删除最后一个逗号。
>
但我不想让这些全球化。
我可以做day2 :: sun并使用它但我收到警告
您可以将枚举放在命名空间中。
命名空间day2
{
enum {s un,mon};
}
-
Ian Collins。
>
" Alf P. Steinbach" < al *** @ start.nowrote in message
news:13 ************* @ corp.supernews.com ...
< blockquote class =post_quotes>
> * Jon Slaughter:
>我正在声明一个枚举,我想限制范围
我知道
enum day2 {sun,mon,};
相当于
静态const int sun = 0;
static const int mon = 1;
嗯,这不是。枚举是一种独特的类型。它的底层积分
类型不一定是int。
这就是微软所说的,而不是我。
>但我不想让这些全球化。
我可以做day2 :: sun并使用它,但我收到关于非标准扩展的警告。它确实有效。
对,这是非标准的。
>我想我可以将它包装在静态类中
没有静态类这样的东西。
>并使用它...也许像
静态struct day2
{
静态const int sun = 0;
static const int mon = 1;
}
缺少变量名称和缺少分号。通过编写静态,你强制编译器假设你正在声明一个变量。
可以在那里定义内联结构,但是你需要在
结束时使用变量名称,无论如何你需要一个分号。
我故意离开分号只是为了打扰你。耶稣基督人!
>
>这似乎有效,但我在第2天遇到没有变量的错误。
如果它没有编译它怎么能工作?
>我想我可以用某种方式解决这个问题,但我想知道是否有其他更好的方法。 (每次使用静态常量int是一个小麻烦,如果没有必要的话)
任何想法?
您可以将枚举包装在类或命名空间中。
类具有以下优点:继承。
命名空间的优点是你可以使用使用。
我想命名空间是个好主意。我已经使用了一个课程,因为
可以用于我的目的。我想虽然使用语法是相同的。
谢谢,
Jon
I''m declaring an enum and I would like to limit the scope
I know that
enum day2 {sun, mon, };
is equivalent to
static const int sun = 0;
static const int mon = 1;
but I''d like not to have these global.
I can do day2::sun and use that but I get a warning
about nonstandard extension. It does work though.
I suppose I can wrap it in a static class and use it... maybe something like
static struct day2
{
static const int sun = 0;
static const int mon = 1;
}
Which seems to work but I get errors about no variables in day2. I suppose I
can fix this in some way but I wondering if there is some other better
method. (using the static constant int every time is a little trouble if its
not necessary)
Any ideas?
Thanks,
Jon
Actualy what I''m doing is
class X
{
enum Y
{
};
};
and it seems to work fine.
Jon Slaughter wrote:I''m declaring an enum and I would like to limit the scope
I know that
enum day2 {sun, mon, };
Drop that last comma.
>
but I''d like not to have these global.
I can do day2::sun and use that but I get a warning
You could put the enum in a namespace.
namespace day2
{
enum { sun, mon };
}
--
Ian Collins.
"Alf P. Steinbach" <al***@start.nowrote in message
news:13*************@corp.supernews.com...>* Jon Slaughter:>I''m declaring an enum and I would like to limit the scope
I know that
enum day2 {sun, mon, };
is equivalent to
static const int sun = 0;
static const int mon = 1;
Well, it isn''t. The enum is a distinct type. And its underlying integral
type need not be int.
This is what microsoft said, not me.
>but I''d like not to have these global.
I can do day2::sun and use that but I get a warning
about nonstandard extension. It does work though.
Right, that''s non-standard.
>I suppose I can wrap it in a static class
There''s no such thing as static class.
>and use it... maybe something like
static struct day2
{
static const int sun = 0;
static const int mon = 1;
}
Missing variable name and missing semicolon. By writing static you force
the compiler to assume that you''re declaring a variable. It''s OK to
define the struct inline there, but you then need a variable name at the
end, and anyway you need a semicolon.
I left the semicolon off on purpose just to bother you. jesus christ man!
>>Which seems to work but I get errors about no variables in day2.
How can it seem to work when it doesn''t compile?
>I suppose I can fix this in some way but I wondering if there is some
other better method. (using the static constant int every time is a
little trouble if its not necessary)
Any ideas?
You can wrap the enum in a class or in a namespace.
A class has the advantage that it can be inherited.
A namespace has the advantage that you can use "using".
I suppose a namespace is a good idea. I used a class already though since
that worked for my purposes. I suppose the usage syntax is identical though.
Thanks,
Jon
这篇关于枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!