Re: enum to string function
Simple way is put a Switch statemetn over the enum values and print it accordingly.
like this
switch(colors)
{
case 1:
cout << "My color is White";
break;
case 2:
cout << "My color is Black"
break;
}
Re: enum to string function
This might be a solution to the approach you mentioned first:
Code:
#include <iostream>
using namespace std;
enum TColor
{
white,
black
};
std::ostream& operator<< (std::ostream& rStream, TColor Color)
{
switch (Color)
{
case white:
{
rStream << "WHITE";
break;
}
case black:
{
rStream << "BLACK";
break;
}
}
return rStream;
}
int main(void)
{
TColor CE;
CE= white;
cout << "Current color= " << CE << endl;
CE= black;
cout << "Current color= " << CE << endl;
return 1;
}
Regards,
Thomas
Re: enum to string function
Wow, thanks a lot, that's exactly what I was looking for. Thx.
Just out of curiosity, why did you name the enum TColor? Is this some convention I don't know about? And what does CE stand for?
Not a very important question, but you seem like the kind of person who knows what he's doing, and I thought I could learn a minor thing or two :)
Re: enum to string function
TSomething is some sort of convention, saying that it is a datatype NOT being a class (i.e. data only, like enum or struct)
CSomeOtherThing would be a class
CE is nothing of this sort: it's short for ColorEnumerator
Regards,
Thomas
Re: enum to string function
Quote:
CE is nothing of this sort: it's short for ColorEnumerator
Then why not just use the name ColorEnumerator? I must admit that I was also rather puzzled as to what CE stood for.
Re: enum to string function
It´s the Delphi naming convention invented by Borland, similar to Microsoft´s MFC naming convention where most classes start with a capital 'C', like 'CWindow' and so on.