I'll start off with a little pointer gymnastics:
// Define the Color class
class Color
{
public:
// declaring an enumeration of color intensities public
// so they can be used by outside parts of the program
enum eLEVEL { OFF=0, DIM=1, MEDIUM=2, BRIGHT=3 };
void red( enum eLEVEL level )
{
_red = level;
}
void green( enum eLEVEL level )
{
_green = level;
}
void blue( enum eLEVEL level )
{
_blue = level;
}
// This private section is down here instead of above the public: above
// to allow for declaration of the enum type value holders. If it were
// above the public... it would not be allowed yet due to the enum being
// declared in the public area.
private:
// Declare the internel colors
enum eLEVEL _red, _green, _blue;
};
int main( int argc, char** argv )
{
// Declare an instance of Color
Color myColor;
// Declaring an array of class method pointers that take an enum parameter
void (Color::*levelMethodsArray[])( enum Color::eLEVEL ) = { &Color::red, &Color::green, &Color::blue };
// Calling a pointer to a method of a class with an
// enum value in a loop setting all colors to DIM
for( int i=0; i < 3; i++ )
((myColor).*(levelMethodsArray[i]))(Color::DIM);
}
|