问题描述
我最近遇到了这个问题:
I recently came across this:
static enum Response{
NO_ERROR=0,
MISSING_DESCRIPTOR,
...
};
它在MSVS2005下编译和工作。但是,我不知道静态修改器应该做什么。它与以下内容有什么不同?
It compiles and works under MSVS2005. However, I'm not sure what the 'static' modifier is supposed to do. Is it any different from the following?
enum Response {
NO_ERROR=0,
MISSING_DESCRIPTOR,
...
};
推荐答案
精确的代码,无效的C ++。您不能在枚举
声明中使用 static
存储类说明符;它不会有任何意义(只有对象,函数和匿名联合可以声明 static
)。
That exact code, with just the ellipsis removed, is not valid C++. You can't use the static
storage class specifier in an enum
declaration; it doesn't make any sense there (only objects, functions, and anonymous unions can be declared static
).
但是,您可以在一个声明中声明枚举
和变量all:
You can, however, declare an enum
and a variable all in one declaration:
static enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
} x;
static
此处适用于 x
,它和你说的是一样的:
The static
here applies to x
and it is effectively the same as if you said:
enum Response {
NO_ERROR = 0,
MISSING_DESCRIPTOR
};
static Response x;
这篇关于C ++:什么是“静态枚举”意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!