Scoped enum as input to function
This should be easy but.. I have the following scoped enum defined
|
enum class Suit{clubs,diamonds,hearts,spades};
|
how do I pass it to a function and do a switch case on it? I'm getting all kind of errors. I've gotten this far:
1 2 3 4 5
|
string suitToString(Suit suit) {
//switch (suit) {
//case
|
1 2 3 4 5 6 7 8 9 10
|
std::string to_string( Suit s ) {
switch(s) {
case Suit::clubs : return "clubs" ;
case Suit::diamonds : return "diamonds" ;
case Suit::hearts : return "hearts" ;
case Suit::spades : return "spades" ;
}
}
|
Or
1 2 3 4 5 6
|
std::string to_string( Suit s ) {
static const std::string text[] { "clubs", "diamonds", "hearts", "spades" };
return text[ int(s) ] ;
}
|
Topic archived. No new replies allowed.