c++ typedef enum和just enum[副本]

时间:2021-01-15 22:30:52

This question already has an answer here:

这个问题已经有了答案:

What exactly is the difference between:

到底有什么不同:

typedef enum {
  something1,
  something2,
  .....
  somethingN
} myEnum;

end just

结束只是

enum myEnum{
  something1,
  something2,
  .....
  somethingN
};

I know in first case I have typedefed unnamed enum, of course, just wonder which approach is better and why?

我知道在第一种情况下我输入了未命名的enum,当然,只是想知道哪种方法更好,为什么?

3 个解决方案

#1


15  

The first variant was useful in C, because otherwise you would have to write enum myEnum everywhere you wished to use it.

第一个变体在C中是有用的,因为否则您必须在希望使用它的任何地方编写myEnum。

This is not the case in C++. So, AFAIK, there is no benefit to the first case in C++ (unless you're defining e.g. an interface that needs to be shared with C).

在c++中不是这样的。因此,AFAIK对于c++中的第一个例子没有任何好处(除非您定义了一个需要与C共享的接口)。

#2


12  

No difference. In fact, the first version is C-style coding.

没有区别。事实上,第一个版本是c风格的编码。

C++11 has introduced stronly-typed enum, which you define as:

c++ 11引入了强类型enum,您将其定义为:

enum class myEnum  //note the 'class' keyword after the 'enum' keyword
{   
  something1,
  something2,
  .....
  somethingN
};

In C++03, enums are not type-safe, they're essentially integers, and can mix with other integral types implicitly, and another problem with them is that they don't have scope; you can use its member without qualifying its type; you can use something1. Whereas C++11 strongly-typed enums are type-safe, and they've scope; you've to use myEnum::something1.

在c++ 03中,枚举不是类型安全的,它们本质上是整数,可以隐式地与其他整数类型混合,另一个问题是它们没有作用域;您可以使用它的成员而不限定其类型;您可以使用something1。而c++ 11强类型的枚举是类型安全的,它们具有作用域;你使用myEnum::something1。

#3


1  

I wouldn't use the first one. It is almost the same as the other one. In C++11, with the second you can write myEnum::something1 but not with the first one. Also in C++11, you can forward declare enums in some cases, but it is impossible to do forward declarations of unamed types and you can't forward declare typedefs either.

我不会用第一个。它几乎和另一个一样。在c++ 11中,有了第二个,您可以编写myEnum::something1,而不是第一个。在c++ 11中,您也可以在某些情况下转发declare enums,但是不可能执行unamed类型的转发声明,也不可能转发声明typedef。

#1


15  

The first variant was useful in C, because otherwise you would have to write enum myEnum everywhere you wished to use it.

第一个变体在C中是有用的,因为否则您必须在希望使用它的任何地方编写myEnum。

This is not the case in C++. So, AFAIK, there is no benefit to the first case in C++ (unless you're defining e.g. an interface that needs to be shared with C).

在c++中不是这样的。因此,AFAIK对于c++中的第一个例子没有任何好处(除非您定义了一个需要与C共享的接口)。

#2


12  

No difference. In fact, the first version is C-style coding.

没有区别。事实上,第一个版本是c风格的编码。

C++11 has introduced stronly-typed enum, which you define as:

c++ 11引入了强类型enum,您将其定义为:

enum class myEnum  //note the 'class' keyword after the 'enum' keyword
{   
  something1,
  something2,
  .....
  somethingN
};

In C++03, enums are not type-safe, they're essentially integers, and can mix with other integral types implicitly, and another problem with them is that they don't have scope; you can use its member without qualifying its type; you can use something1. Whereas C++11 strongly-typed enums are type-safe, and they've scope; you've to use myEnum::something1.

在c++ 03中,枚举不是类型安全的,它们本质上是整数,可以隐式地与其他整数类型混合,另一个问题是它们没有作用域;您可以使用它的成员而不限定其类型;您可以使用something1。而c++ 11强类型的枚举是类型安全的,它们具有作用域;你使用myEnum::something1。

#3


1  

I wouldn't use the first one. It is almost the same as the other one. In C++11, with the second you can write myEnum::something1 but not with the first one. Also in C++11, you can forward declare enums in some cases, but it is impossible to do forward declarations of unamed types and you can't forward declare typedefs either.

我不会用第一个。它几乎和另一个一样。在c++ 11中,有了第二个,您可以编写myEnum::something1,而不是第一个。在c++ 11中,您也可以在某些情况下转发declare enums,但是不可能执行unamed类型的转发声明,也不可能转发声明typedef。