Conceptually, enums and consts share a lot of similarities. Both enable better readability of code.
They also allow for the actual value being represented to be maintained at one location in your code,
thus enabling the value to be changed at one location and have that change reflected throughout the
code. Where enums and consts differ is that enums allow for grouping of common values within a
single construct, creating a new data type. Then you can use this new data type to enforce that only specific enum values be placed in a specified variable. A const, on the other hand, is just a representation
of a value and cannot be used to define a new data type.
Like the fundamental types already discussed, enums default to being placed on the stack but
can beused automatically as objects when required.
There are two different syntaxes for declaring enums in C++/CLI. Ultimately, both syntax
generate the same metadata and inherit from System::enums.
The first syntax is the pre-C++/CLI style, better known as a native enum. C++/CLI has augmented
the native enum with the addition of an optional ability to declare the underlying data type. The data
type of a native enum can be explicitly declared as one of the following data types: bool, char, unsigned
char, signed char, short, unsigned short, int, unsigned int, long long, unsigned long long, float,
or double. Here is an example of a native enum with and without the optional declaration of the data type:
enum Creature { Dog, Cat, Eagle };
enum Vehicle : char { Car, Boat, Plane };
The second syntax, know as CLI enums, is the preferred one for managed code (according to
Microsoft) and mirrors more the syntax of the other value type declarations:
enum class Creature { Dog, Cat, Eagle };
enum struct Creature { Dog, Cat, Eagle };
CLI enums are different from native enums in that the names of the CLI enums’ values, better
known as enumerators, can only be found through the scope of the enums’ name, and the declaring
of the enums’ data type has no meaning with a CLI enum. What this means to you is that to code a
native enum like this:
Creature animal;
animal = Cat;
you code a CLI enum like this:
Creature animal;
animal = Creature::Cat;
The following example creates a CLI enum of all the primary colors. Then the function prints the
string equivalent of the primary color enum using a switch statement. I describe the switch statement
later in this chapter.
The System::Enum from which enums originate provides a simpler way of doing this exact same
thing. The ToString() method for enums prints out the enum name as a character string.
Listing 2-7 is a little program showing enums in action.
using namespace System;
enum class PrimeColors { Red, Blue, Yellow };
// Enum Type in Action
void main()
{
PrimeColors color;
color = PrimeColors::Blue;
switch (color)
{
case PrimeColors::Red :
Console::WriteLine("Red");
break;
case PrimeColors::Blue :
Console::WriteLine("Blue");
break;
case PrimeColors::Yellow :
Console::WriteLine("Yellow");
break;
}
Console::WriteLine(color.ToString());
}
They also allow for the actual value being represented to be maintained at one location in your code,
thus enabling the value to be changed at one location and have that change reflected throughout the
code. Where enums and consts differ is that enums allow for grouping of common values within a
single construct, creating a new data type. Then you can use this new data type to enforce that only specific enum values be placed in a specified variable. A const, on the other hand, is just a representation
of a value and cannot be used to define a new data type.
Like the fundamental types already discussed, enums default to being placed on the stack but
can beused automatically as objects when required.
There are two different syntaxes for declaring enums in C++/CLI. Ultimately, both syntax
generate the same metadata and inherit from System::enums.
The first syntax is the pre-C++/CLI style, better known as a native enum. C++/CLI has augmented
the native enum with the addition of an optional ability to declare the underlying data type. The data
type of a native enum can be explicitly declared as one of the following data types: bool, char, unsigned
char, signed char, short, unsigned short, int, unsigned int, long long, unsigned long long, float,
or double. Here is an example of a native enum with and without the optional declaration of the data type:
enum Creature { Dog, Cat, Eagle };
enum Vehicle : char { Car, Boat, Plane };
The second syntax, know as CLI enums, is the preferred one for managed code (according to
Microsoft) and mirrors more the syntax of the other value type declarations:
enum class Creature { Dog, Cat, Eagle };
enum struct Creature { Dog, Cat, Eagle };
CLI enums are different from native enums in that the names of the CLI enums’ values, better
known as enumerators, can only be found through the scope of the enums’ name, and the declaring
of the enums’ data type has no meaning with a CLI enum. What this means to you is that to code a
native enum like this:
Creature animal;
animal = Cat;
you code a CLI enum like this:
Creature animal;
animal = Creature::Cat;
The following example creates a CLI enum of all the primary colors. Then the function prints the
string equivalent of the primary color enum using a switch statement. I describe the switch statement
later in this chapter.
The System::Enum from which enums originate provides a simpler way of doing this exact same
thing. The ToString() method for enums prints out the enum name as a character string.
Listing 2-7 is a little program showing enums in action.
using namespace System;
enum class PrimeColors { Red, Blue, Yellow };
// Enum Type in Action
void main()
{
PrimeColors color;
color = PrimeColors::Blue;
switch (color)
{
case PrimeColors::Red :
Console::WriteLine("Red");
break;
case PrimeColors::Blue :
Console::WriteLine("Blue");
break;
case PrimeColors::Yellow :
Console::WriteLine("Yellow");
break;
}
Console::WriteLine(color.ToString());
}



浙公网安备 33010602011771号