Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The use of enum without a variable name

Tags:

c++

I understand the first one but the second one? When and why would you do that?

enum cartoon { HOMER, MARGE, BART, LISA, MAGGIE };  enum { HOMER, MARGE, BART, LISA, MAGGIE }; 
like image 772
RoR Avatar asked Jan 07 '11 07:01

RoR


People also ask

What is the use of enum variable?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is the use of enum function?

Enumeration (or enum) is a user defined data type in C. It is mainly used to assign names to integral constants, the names make a program easy to read and maintain.

Can enum have variables?

Methods and variables in an enumeration Enumerations are similar to classes and, you can have variables, methods, and constructors within them. Only concrete methods are allowed in an enumeration.

What is the use of enum in node JS?

js. - [Instructor] Enums are a great way to set a series of values that don't change or are immutable. For example, it could be used for a settings files.


2 Answers

you can define such enum in a class, which gives it a scope, and helps expose class's capabilities, e.g.

class Encryption { public:   enum { DEFAUTL_KEYLEN=16, SHORT_KEYLEN=8, LONG_KEYLEN=32 };   // ... };  byte key[Encryption::DEFAUTL_KEYLEN]; 
like image 128
davka Avatar answered Oct 09 '22 04:10

davka


This simply creates constant expressions that have the respective values, without a type other than simply int, so you can't use an unnamed enum as a type for anything, you will have to simply accept an int and then do comparison with the constant.

Something like this:

void doSomething(int c); 

Versus something like this:

void doSomething(cartoon c); 

By the way, your implementation would be the same, so in the case of unnamed enums, the only real drawback is strong typing.

like image 26
Jacob Relkin Avatar answered Oct 09 '22 03:10

Jacob Relkin