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 };
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.
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.
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.
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.
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];
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With