enum in class

L

Larry I Smith

Teddy said:
what is the special meaning of enum members in class ?

An example:

--- en.cpp ---

// a global enum named MyColor
enum MyColor { Red, Green, Blue };

// a simple class with an enum and a variable of that
// enum type
class Test
{
public:
enum TestEnum
{
value1,
value2
};

TestEnum enumVar;
};

int main()
{
// a variable named 'clr' of type 'MyColor'
// with a value of 'Green'
MyColor clr = Green;

// a Test object named 'tst'
Test tst;

// a variable named 'value' of type 'Test::TestEnum'
// with a value of 'Test::value2'
Test::TestEnum value = Test::value2;

// assign the value 'Test::value1' to the
// 'enumVar' member of 'tst'
tst.enumVar = Test::value1;

return 0;
}


Regards,
Larry
 
L

Larry I Smith

Larry said:
An example:

--- en.cpp ---

// a global enum named MyColor
enum MyColor { Red, Green, Blue };

// a simple class with an enum and a variable of that
// enum type
class Test
{
public:
enum TestEnum
{
value1,
value2
};

// a constructor to complete the example.
// sets 'enumVar' to 'value2' when a
// Test object is created
Test() : enumVar(value2) {}
TestEnum enumVar;
};

int main()
{
// a variable named 'clr' of type 'MyColor'
// with a value of 'Green'
MyColor clr = Green;

// a Test object named 'tst'
Test tst;

// a variable named 'value' of type 'Test::TestEnum'
// with a value of 'Test::value2'
Test::TestEnum value = Test::value2;

// assign the value 'Test::value1' to the
// 'enumVar' member of 'tst'
tst.enumVar = Test::value1;

return 0;
}


Regards,
Larry

To complete the example, I added a default constructor above.
The example now demonstrates most of the rules for using
global and class enums.

Regards,
Larry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top