C ++:什么是“静态枚举”意思 [英] C++: what does "static enum" mean

查看:145
本文介绍了C ++:什么是“静态枚举”意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近遇到了这个问题:

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 ++:什么是“静态枚举”意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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