Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous enum classes

Tags:

c++

enums

c++11

Is is possible to write a anonymous enum class and then comparing what it contains? Eg.

enum class { APPLE, BANANA } fruitType;
// ...
if (fruitType == fruitType::APPLE)
    // ...
like image 672
小太郎 Avatar asked Aug 03 '11 06:08

小太郎


People also ask

What is anonymous enum?

An anonymous Enum is an unnamed enum.

What is an enum class?

An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables). To create an enum , use the enum keyword (instead of class or interface), and separate the constants with a comma.

What is enum class in C++?

An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. Note. This article covers the ISO Standard C++ Language enum type and the scoped (or strongly-typed) enum class type which is introduced in C++11.

What is Kotlin enum class?

Kotlin enums are classes, which means that they can have one or more constructors. Thus, you can initialize enum constants by passing the values required to one of the valid constructors. This is possible because enum constants are nothing other than instances of the enum class itself.


1 Answers

No, fruitType is a variable (despite Type in the name). You cannot use a variable name to access things about its type.

The idea with enum class is that the values are not visible outside the definition unless you prefix them with the type name. If the type doesn't have a name, this will be difficult!

like image 153
Bo Persson Avatar answered Oct 09 '22 12:10

Bo Persson