Thread: Using enums in classes

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    89

    Using enums in classes

    I have been reading some tutorials and books on c++ and I noticed that the authors almost always place an enum declaration outiside a class declaration, but dont explain why.
    Code:
    CPerson.h
    
    
    enum PersonState
    {
         PersonAwake = 1,
         PersonHungry = 2,
         PersonTired = 3,
         PersonAsleep = 4
    };
    
    class CPerson
    {
         public:
         // Person methods 
         private:
         // Person members
    };
    So I guess my question is: Is it better to do it this way instead of putting the enum inside the class declaration?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I don't know about why they do that, but it you place it inside, then you must type the class name to access it like -> CPerson::PersonState.
    If the enum is for the class only, then I usually do so. By putting it inside the class, you can also make it private - thus, none but the class can use the enum.
    If you're lazy, you can always use a typedef -> typedef CPerson::PersonState PersonState;
    Then you can use the enum without typing CPerson:: first.
    I don't really see a reason why. Putting it inside a class also makes it stick in like a namespace - so you can use the same name for the enum over and over.

    All in all, I recommend placing all such things inside the classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    89
    Thanks
    I have always placed enums inside classes anyway. I just wondered if there was a certian reason for it.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Just another thought.
    If you make the enum without a name ( which is probably pointless then the enum values will be accessed just like a public variable.
    Code:
    class CSomething
    {
    private:
        //
    public:
       enum
       {
          A = 0,
          B,
          C
       };
    };
    
    //Then access the enum
    if ( SomeInt == CSomething::A )
       return 0;
    //Or something like that, or however your going to use it
    Hope that helps alittle at least.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, which kinda defines the purpose of an enum. Enums are mostly used to define a set of values a specific parameter can hold. And if the enum has no name... well, then you can't define such a specific variable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    IMHO, enums should always be contained by a class or namespace. Otherwise, you will have name conflicts just like you get between other types of identifiers. In other words, instead of:

    Code:
    enum CarModel
    {
        Toyota,
        BMW,
        Ford
    };
    
    class Car
    {
    public:
        Car(CarModel model_p) : model(model_p) { }
    private:
        CarModel model;
    };
    You should instead place the enumeration inside the class:

    Code:
    class Car
    {
    public:
        enum Model { Toyota, BMW, Ford };
        Car(Model model_p) : model(model_p) { }
    private:
        Model model;
    };
    This way, external code will refer to Car::Toyota instead of just Toyota. If the enumeration is conceptually a part of a class, or used to describe objects of a class, then it should be contained in the class.

    A "bare" enumeration which is not closely related to any single class should at least go into a namespace, not the global namespace. For instance declaring an enum with values Yes, No, Maybe is highly likely to conflict with some other enumeration.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 09:04 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM