Thread: store enum

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    store enum

    Hello..

    What is the best way to store enum? I usually use unsigned short..
    What about you guys?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    What is wrong with enums themselves?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    Do you mean:

    Code:
    enum e_num { 
    	SOME_ENUM,
    	ENUM_2
    };
    
    e_num test = ENUM_2;

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    I think we really can call this thing enum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,364
    Code:
    typedef enum {
      RED,
      GREEN,
      BLUE
    } RGBColor;
    
    RGBColor myRGBColor = BLUE;
    If you need to store an enum value as a regular type, use int (or unsigned int). The only reason to use short would be if you were storing a huge number of them and needed to save the space.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by l2u
    Hello..

    What is the best way to store enum? I usually use unsigned short..
    What about you guys?
    If you mean as just a variable in your code, use the type of the enum (e.g. e_num as above). If you mean for transmission across the wire, use whatever is big enough.
    Otherwise you'll have to clarify what you mean by "store".

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    921
    Code:
    enum RGBColor {
      RED,
      GREEN,
      BLUE
    };
    
    RGBColor myRGBColor = BLUE;
    works as well.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    In C++; not in C. In C you must specify enum before the tag name, or use a typedef.

    Of course, this is the C++ forum, but it's still good to know.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A basic question on enum
    By MasterM in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2009, 09:16 PM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 02:27 PM
  4. Conflicting enum issue
    By 7force in forum C Programming
    Replies: 1
    Last Post: 07-05-2006, 03:51 AM
  5. Switch case and enum help
    By SomeCrazyGuy in forum C++ Programming
    Replies: 9
    Last Post: 04-21-2005, 08:53 PM