The typical way to define an integer constant to use inside a function is:
const int NumbeOfElements = 10;
the same for using within a class:
class Class {
...
static const int NumberOfElements = 10;
};
It can then be used as a fixed-size array bound which means it is known at compile time.
Long ago compilers didn't support the latter syntax and that's why enums were used:
enum NumberOfElementsEnum { NumberOfElements = 10; }
Now with almost every widely used compiler supporting both the in-function const int
and the in-class static const int
syntax is there any reason to use the enum for this purpose?
The use of an enumeration constant (enum) has many advantages over using the traditional symbolic constant style of #define. These advantages include a lower maintenance requirement, improved program readability, and better debugging capability.
Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.
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.
Enums limit you to the required set of inputs whereas even if you use constant strings you still can use other String not part of your logic. This helps you to not make a mistake, to enter something out of the domain, while entering data and also improves the program readability.
The reason is mainly brevity. First of all, an enum
can be anonymous:
class foo {
enum { bar = 1 };
};
This effectively introduces bar
as an integral constant. Note that the above is shorter than static const int
.
Also, no-one could possibly write &bar
if it's an enum
member. If you do this:
class foo {
static const int bar = 1;
}
and then the client of your class does this:
printf("%p", &foo::bar);
then he will get a compile-time linker error that foo::bar
is not defined (because, well, as an lvalue, it's not). In practice, with the Standard as it currently stands, anywhere bar
is used where an integral constant expression is not required (i.e. where it is merely allowed), it requires an out-of-class definition of foo::bar.
The places where such an expression is required are: enum
initializers, case
labels, array size in types (excepting new[]
), and template arguments of integral types. Thus, using bar
anywhere else technically requires a definition. See C++ Core Language Active Issue 712 for more info - there are no proposed resolutions as of yet.
In practice, most compilers these days are more lenient about this, and will let you get away with most "common sense" uses of static const int
variables without requiring a definition. However, the corner cases may differ, however, so many consider it to be better to just use anonymous enum
, for which everything is crystal clear, and there's no ambiguity at all.
Defining static constants directly in the class definition is a later addition to C++ and many still stick to the older workaround of using an enum
for that. There might even be a few older compilers still in use which don't support static constants directly defined in class definitions.
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