Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between enum struct and enum class?

Tags:

c++

enums

c++11

Looking at the enum documentation, there was one thing that I noticed:

enum-key - one of enum, enum class(since C++11), or enum struct(since C++11)

enum and enum class, sure, but what is a enum struct?

The docs seem to say that enum class and enum struct are exactly the same:

[...] scoped enumeration (declared with the enum-key enum class or enum struct)


  • enum struct|class name { enumerator = constexpr , enumerator = constexpr , ... }
  • [...]

Are they really exactly the same? Or are there any differences that I missed? What is the point (if they are the same) to have 2 different syntax for the same thing?

like image 893
Rakete1111 Avatar asked Jul 22 '16 23:07

Rakete1111


People also ask

Is there a difference between class and struct?

A class is a user-defined blueprint or prototype from which objects are created. Basically, a class combines the fields and methods(member function which defines actions) into a single unit. A structure is a collection of variables of different data types under a single unit.

What is enum and struct in C++?

In this article, we will discuss structures, unions, and enumerations and their differences. The structure is a user-defined data type that is available in C++. Structures are used to combine different types of data types, just like an array is used to combine the same type of data types.

What is the difference between enum and enum class in C++?

An enum just spills its contents into the enclosing scope, and is basically a const static integer. This means that the first element of any default enum is the same using the == operator. Enum classes have their own scope, and don't pollute the namespace that they are in.

Can we use enum inside the structure?

Declaring the enum inside the struct is fine and the way you've laid it out is how I did it as well. You won't be able to access the enum Status outside of the Task struct, but for this challenge that's not something to be concerned about. Posting to the forum is only allowed for members with active accounts.


Video Answer


1 Answers

enum class and enum struct are the same (emphasis mine).

7.2 Enumeration declarations
...
2 .... The enum-keys enum class and enum struct are semantically equivalent; an enumeration type declared with one of these is a scoped enumeration, and its enumerators are scoped enumerators.

like image 68
AlexD Avatar answered Sep 20 '22 06:09

AlexD