Why should I have an enumeration declared using typedef in C ++?

I had code that looked like this:

enum EEventID {
  eEvent1,
  eEvent2,
  ...
  eEventN };

And it was reviewed and changed to

typedef enum {
  eEvent1,
  eEvent2,
  ...
  eEventN } EEventID;

What is the difference between the two? Why make a change? When I looked at this question , the only mention of typedefs was omitted.

+5
source share
5 answers

Both are identical in C ++, but they are not the same in C - in C, if you use typedef, you get code compatible between C and C ++ (therefore, it can be used freely in the header file, which can be for C or C + +). This is the only reason I may prefer it.

+11
source

: ", C, , enum , , enum struct ."

, - C-, , .

+2

, . C, .

+2

typedef is superfluous in C ++ .
The C is necessary if you want to determine your type of the variables listed in the following way: EEventID event;otherwise you would have to specify its transfer: enum EEventID event;.

+2
source

Your reviewer does not know his C from his C ++.

0
source

All Articles