|
-
September 25th, 2008, 04:26 AM
#1
enum to string function
I have this basic program:
Code:
enum Color {Black, White};
Color myColor = White;
std::cout << "My color is " << myColor << std::endl
with out put: "Mycolor is 1".
What I want is: "My color is White".
I'm having trouble implementing a function to do this. I can see several (3) solutions:
1. The best: Finding a way to overcharge the operator "<<" when applied on an "Color" object, to return a "string". I'm not sure at all how to do this.
2. Write a function which would basically be "String Color2String(Color)". This would allow me to simply write:
std::cout << "My color is " << Color2String(myColor) << std::endl
However, I am not really sure how to return the string item, without creating a memory leak. Would it be possible maybe to define it as a member of the string class, and write this?:
std::cout << "My color is " << String::Color2String(myColor) << std::endl
3. This solution would just consist in putting a string as function input parameter. I do not like this solution, because it would require a reprocess line of code (or more in case of multiple colors), as such:
Color2String(myString, myColor);
std::cout << "My color is " << myString << std::endl
The actual core of the function would be nothing more than a simple switch statement, but I'm having trouble declaring my function, and returning the result. Can I get some help please? Thank you for your time
-
September 25th, 2008, 05:49 AM
#2
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;
}
-
September 25th, 2008, 06:10 AM
#3
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
-
September 25th, 2008, 06:41 AM
#4
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
-
September 25th, 2008, 07:06 AM
#5
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
-
September 25th, 2008, 07:34 AM
#6
Re: enum to string function
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.
-
September 25th, 2008, 07:39 AM
#7
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.
- Guido
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|