I saw this construct in the project I work:
const enum SomeEnum
{
val0,
val1,
val2
};
What is the purpose of const
here?
A const Enum is the same as a normal Enum. Except that no Object is generated at compile time. Instead, the literal values are substituted where the const Enum is used.
"Plain" enums and enum class objects both are of integral type, so the decision of passing by const reference or by value is just the same as if you did for other arguments of integral type.
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.
An enumeration is a user-defined type that consists of a set of named integral constants that are known as enumerators. 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.
Nothing at all. Actually, according to G++ it is a compiler error:
error: qualifiers can only be specified for objects and functions
However, in C it is allowed, but useless. GCC says:
warning: useless type qualifier in empty declaration
The issue is that const
only applies to objects (variables) and member functions, but not to basic types.
It doesn't make a difference in your code, but it would in this case:
const enum SomeEnum
{
val0,
val1,
val2
} VAL0 = val0;
Here, VAL0
would be a const
variable (with the value val0
). TBH though, it isn't of much use.
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