store enum

This is a discussion on store enum within the C++ Programming forums, part of the General Programming Boards category; Hello.. What is the best way to store enum? I usually use unsigned short.. What about you guys?...

  1. #1
    l2u
    l2u is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    What is wrong with enums themselves?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    l2u
    l2u is offline
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I think we really can call this thing enum
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    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,308
    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
    907
    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,048
    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, 02:55 AM
  3. Free Store of memory
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 11-12-2007, 01: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21