CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Mar 2008
    Posts
    2

    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

  2. #2
    Join Date
    Sep 2008
    Posts
    48

    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;
    }

  3. #3
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    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

  4. #4
    Join Date
    Mar 2008
    Posts
    2

    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

  5. #5
    Join Date
    Aug 1999
    Location
    Darmstadt, FRG
    Posts
    87

    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

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    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
  •  





Click Here to Expand Forum to Full Width

Featured