Just a quick question on classes and enums
Just wondering when you use a enumerator in a class
eg enum date
class image
{
public:
private:
date adate:
};
Do you access the enum value by a image::image call ?
or does enum value stay as its own class?
thanks
Brendan
It depends. Pay attention to lines 9 and 17 of the following two examples.
In this case, the suit type is not related to the class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
enum t_suit {spades, hearts, diamonds, clubs};
class t_card
{
public:
t_suit suit;
// constructor sets 'suit' to 'hearts'
t_card(): suit( hearts ) { }
};
int main()
{
t_card card;
// but we'd rather it be 'spades'
card.suit = spades;
}
| |
But in this case, the suit type is part of the class type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
class t_card
{
public:
enum t_suit {spades, hearts, diamonds, clubs};
t_suit suit;
// constructor sets 'suit' to 'hearts'
t_card(): suit( hearts ) { }
};
int main()
{
t_card card;
// but we'd rather it be 'spades'
card.suit = t_card::spades;
}
| |
Hope this helps.
Topic archived. No new replies allowed.