Another static_cast demonstrating the integer to enum type casting in C++



Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Converting the integer type to enum type using the C++ type caster

To show: Using the static_cast C++ type caster




#include <iostream>

using namespace std;


// enum data type

enum color {blue, yellow, red, green, magenta};


int main(void)


int p1 = 3;


cout<<"integer type, p1 = "<<p1<<endl;

cout<<"color c1 = static_cast<color> (p1)"<<endl;

color c1 = static_cast<color> (p1);

cout<<"enum type, c1 = "<<c1<<endl;

return 0;



Output example:


integer type, p1 = 3

color c1 = static_cast<color> (p1)

enum type, c1 = 3

Press any key to continue . . .



C and C++ Programming Resources | C & C++ Code Example Index