enumeration design question

This is a discussion on enumeration design question within the C++ Programming forums, part of the General Programming Boards category; Hello When you use enum inside a class, how do you implement it? Suppose you have: Code: class someclass { ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    enumeration design question

    Hello

    When you use enum inside a class, how do you implement it?

    Suppose you have:

    Code:
    class someclass {
    public:
      enum state_type {
         work = 1,
         sleep
      };
     
      state_type m_state;
    };
    
    m_state = work;
    or would you put enum state_type in a structure (for instance struct state) so you could write:
    Code:
    state::state_type somestate = state::work;
    which looks nicer if you ask me.

    What do you think?

    Thanks for answers!

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just use an anonymous enumeration (a fancy word for not giving it a name)

    Example:
    Code:
    class state {
    public:
      enum {
         Arkansas = 1,
         Alaska,
         California
      };
    };
    Tada!
    Last edited by master5001; 04-11-2008 at 03:33 PM.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by master5001 View Post
    Just use an anonymous enumeration (a fancy word for not giving it a name)

    Example:
    Code:
    class state {
    public:
      enum {
         Arkansas = 1,
         Alaska,
         California
      };
    };
    Tada!
    How do you define it this way?
    Code:
    state blah = state::Alaska;
    ?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    class state {
    public:
      enum {
         Undefined = 0,
         Arkansas = 1,
         Alaska,
         California
      };
    
    protected:
      int value;
    
    public:
      state() : value(Undefined) { }
      state(int val) : value(val) { }
      state(const state &o) : value(o.value) { }
    
      state operator=(int val)
      {
         value = val;
      }
    };
    This kind of allows you to do what you want from all angles.. Though its a lot of work for such little gain.... You deserve a job at Microsoft

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not put it in a namespace?
    Code:
    namespace Food
    {
       enum Vegetables
       {
          Onion = 1,
          Potato = 2
       };
    
       enum Fruits
       {
          Apple = 1,
          Orange = 2
       };
    }

  6. #6
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    class state {
    public:
      enum {
         Undefined = 0,
         Arkansas = 1,
         Alaska,
         California
      };
    
    protected:
      int value;
    
    public:
      state() : value(Undefined) { }
      state(int val) : value(val) { }
      state(const state &o) : value(o.value) { }
    
      state operator=(int val)
      {
         value = val;
      }
    };
    This kind of allows you to do what you want from all angles.. Though its a lot of work for such little gain.... You deserve a job at Microsoft
    Is there any way to make state class more flexibile, so I wouldnt have to write it for each different enum?

    Maybe templated class, something like:
    Code:
    enum test_enum {
      bla = 1,
      blabla = 2
    };
    
    template <typename T>
    class state {
    public:
    	typedef enum T s;
    
    protected:
    	int value;
    
    public:
    	state() : value(0) { }
    	state(int val) : value(val) { }
    	state(const state &o) : value(o.value) { }
    
    	state operator=(int val) {
    		value = val;
    	}
    };
    and then:
    Code:
    state<test_enum> test;
    test = state::bla;
    Is this possible?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What is the REAL problem you are trying to solve?

    All of the above solutions, plus several handfuls more could be used - it's all about what you are trying to achieve - here's my suggestions to solving some of the possible problems you may be looking at:
    1. Isolation/namespace: just make sure that the "work" is not cluttering up the global namespace, use either a class or namespace (I prefer the namespace solution myself).
    2. Ensuring correct use: wrap the state in a class, so that it's "impossible" to misuse the state.
    3. Something else? No suggestion until further details of why you want to do this.

    My personal feeling is that "don't make things more complex for no reason" - so if there's a good reason for putting the enum in a namespace, then do so. If not, then don't. If there's a good reason to wrap the enum in a class, then do so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by matsp View Post
    What is the REAL problem you are trying to solve?

    All of the above solutions, plus several handfuls more could be used - it's all about what you are trying to achieve - here's my suggestions to solving some of the possible problems you may be looking at:
    1. Isolation/namespace: just make sure that the "work" is not cluttering up the global namespace, use either a class or namespace (I prefer the namespace solution myself).
    2. Ensuring correct use: wrap the state in a class, so that it's "impossible" to misuse the state.
    3. Something else? No suggestion until further details of why you want to do this.

    My personal feeling is that "don't make things more complex for no reason" - so if there's a good reason for putting the enum in a namespace, then do so. If not, then don't. If there's a good reason to wrap the enum in a class, then do so.

    --
    Mats
    The reason is I have an enum that is being used by two projects (in the same workspace) so I need to have it in shared files and I want to have everything organized as much as possible.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd use a namespace for that purpose. It gives the "isolation" (that is, this enum will never collide with another enum defined locally in either of the two projects), at the same time as there is no unnecessary overhead.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    I'm having problems with how to name it properly..

    This enum is enum for client state..

    So if I put it in a namespace client this way:

    Code:
    namespace client {
      enum state { standby = 1 };
    };
    
    client::state some_state = client::standby;
    or:

    Code:
    namespace client_state {
      enum state { standby = 1 };
    };
    
    client_state::state some_state = client_state::standby;
    Which way do you prefer? I would prefer client::state::standby if that would be possible thats why I've been considering above approach with putting it in a class..

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    state gets repeated in your second example, which doesn't add any useful information. Not adding anything of value -> don't use it. My personal view, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by matsp View Post
    state gets repeated in your second example, which doesn't add any useful information. Not adding anything of value -> don't use it. My personal view, of course.

    --
    Mats
    Thanks a lot Mats!

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    One other point: You may find that OTHER things that are shared can go into the same namespace, and that would of course be more difficult if it refers "too much" to the content of the namespace. Namespaces should be used to isolate UNRELATED names/symbols, not to keep a single enum, class or type declaration, as a general rule.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Just being curious.. Is it possible to the template class I posted above work with enums like I wanted so that you could make any enum as class's enum?

  15. #15
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    In other words, I would like to be able to do this (to somehow derive enum in a class):

    Code:
    enum states { buu };
    
    class test_class {
    public:
    	typedef enum states t;
    };
    
    test_class::t sample = test_class::buu;
    Is this possible?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game design question
    By h3ro in forum Game Programming
    Replies: 6
    Last Post: 02-28-2008, 07:20 AM
  2. Question about engine design.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 01-29-2008, 09:53 AM
  3. question about class design and overloading
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2007, 01:02 PM
  4. database app design question
    By MPSoutine in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2003, 09:13 PM
  5. design question: opinion
    By ggs in forum C Programming
    Replies: 2
    Last Post: 01-29-2003, 10:59 AM

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