在这种情况下,匿名枚举应该使用? [英] in which situations anonymous enum should be used?
问题描述
在C和C ++ 枚举
可以与标签来定义
In both c and c++ enum
could be defined with tag
enum e_smth {
smth_one,
smth_two,
smth_err
};
或无标签
enum {
smth_one,
smth_two,
smth_err
};
如果有人用标签来定义它是有道理的开关
在C和C ++语句:
e_smth some_var;
....//do some stuff with some_var
switch (some_var)
{
case smth_one:
break;
case smth_two:
break;
}
将产生 -Wswitch
警告这是否会用gcc或g ++编译。
will produce -Wswitch
warning if this will be compiled with gcc or g++.
这是有道理的函数的声明和变量初始化在C ++:
It makes sense in functions declarations and variables initialization in c++ :
e_smth var;
var=99;
如果使用g ++编译会产生 -fpermissive
错误。
Will produce -fpermissive
error if compiled with g++.
这两种类型的有或无标签可以被用作一个文件的#define
宏没有参数。
Both types with or without tag could be used as one file #define
macro without parameters.
更新
可以作为一个文件的#define
宏不带参数
could be used as one file
#define
macro without parameters
意味着:如果不希望编写的#define MAX 1000
中的文件并添加MAX全球使用枚举{MAX = 1000} $ C $的C>刚刚在文件范围
meant : Instead of writing #define MAX 1000
in file and adding MAX to global use enum { MAX=1000 }
just in file scope
和有关匿名枚举,我发现了什么只有一个用例:
定义,就像的typedef枚举{A,B,C} some_t;
使得它像标签枚举工作
And what about anonymous enums, I've found just one use cases:
definition like typedef enum { a,b,c } some_t;
makes it work like enum with tag
如果我还没有描述的所有合理使用的情况下,应该使用什么匿名枚举?
if I haven't yet described all reasonable use cases, what for anonymous enums should be used?
推荐答案
在C(而不是在C ++),枚举
可以是[AB]用于定义 INT
常量。
In C (but not in C++), enum
can be [ab]used to define int
constants.
例如,鉴于这一声明:
const int MAX = 1024;
MAX
不是恒定的前pression,这是一个只读对象的名称。这意味着你不能在标签的情况下使用它,作为一个数组的大小在文件范围内或者与静态
,或以任何其他情况下需要不断前$声明p $ pssion。
MAX
is not a constant expression, it's the name of a read-only object. That means you can't use it in a case label, as the size of an array declared at file scope or with static
, or in any other context requiring a constant expression.
但是,如果你写的:
enum { MAX = 1024 };
然后 MAX
的是的类型的常量前pression INT
,可用在任何情况下,你可以使用常数 1024
。
then MAX
is a constant expression of type int
, usable in any context where you could use the constant 1024
.
当然,你也可以写:
#define MAX 1024
但也有缺点使用preprocessor:标识符的作用域,将被赋予一个普通的申报方式,例如:
but there are disadvantages to using the preprocessor: the identifier isn't scoped the way it would be given an ordinary declaration, for example.
的缺点是,这样一个恒定的只能是类型 INT
的
The drawback is that such a constant can only be of type int
.
C ++有不同的规则;枚举常量是枚举类型,而不是 INT
的,但您可以使用声明的常量对象恒定前pressions(只要初始值是恒定的前pression)。
C++ has different rules; enumeration constants are of the enumerated type, not int
, but you can use declared constant objects as constant expressions (as long as the initializer is a constant expression).
要解决原来的问题,当您使用枚举
声明来创建这样的常量,有一个在具有或者标签或一个typedef没有意义的,因为你从不使用该类型本身。
To address the original question, when you use an enum
declaration to create constants like this, there's no point in having either a tag or a typedef, since you'll never use the type itself.
背景:
enum foo { zero, one, two };
enum foo obj = two;
创建类型为枚举富
和常量零
,有一个
和两个
。在C中,常量总是键入 INT
,这是无可否认奇,和初始化 OBJ
涉及的隐转化率从 INT
到枚举富
。
creates a type enum foo
and constants zero
, one
, and two
. In C, the constants are always of type int
, which is admittedly odd, and the initialization of obj
involves an implicit conversion from int
to enum foo
.
在C ++中,键入枚举富
,也可以简称为富
和常量的键入枚举富
(这是兼容的部分的整数类型,不一定 INT
)。
In C++, the type enum foo
can also be referred to as just foo
, and the constants are of type enum foo
(which is compatible with some integer type, not necessarily int
).
这篇关于在这种情况下,匿名枚举应该使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!