How to use enums in C++

前端 未结 14 510
臣服心动
臣服心动 2020-12-04 04:30

Suppose we have an enum like the following:

enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday};

I want to crea

相关标签:
14条回答
  • 2020-12-04 05:16

    I think your root issue is the use of . instead of ::, which will use the namespace.

    Try:

    enum Days {Saturday, Sunday, Tuesday, Wednesday, Thursday, Friday};
    Days day = Days::Saturday;
    if(Days::Saturday == day)  // I like literals before variables :)
    {
        std::cout<<"Ok its Saturday";
    }
    
    0 讨论(0)
  • 2020-12-04 05:19

    Sadly, elements of the enum are 'global'. You access them by doing day = Saturday. That means that you cannot have enum A { a, b } ; and enum B { b, a } ; for they are in conflict.

    0 讨论(0)
  • 2020-12-04 05:20

    Much of this should give you compilation errors.

    // note the lower case enum keyword
    enum Days { Saturday, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday };
    

    Now, Saturday, Sunday, etc. can be used as top-level bare constants,and Days can be used as a type:

    Days day = Saturday;   // Days.Saturday is an error
    

    And similarly later, to test:

    if (day == Saturday)
        // ...
    

    These enum values are like bare constants - they're un-scoped - with a little extra help from the compiler: (unless you're using C++11 enum classes) they aren't encapsulated like object or structure members for instance, and you can't refer to them as members of Days.

    You'll have what you're looking for with C++11, which introduces an enum class:

    enum class Days
    {
        SUNDAY,
        MONDAY,
        // ... etc.
    }
    
    // ...
    
    if (day == Days::SUNDAY)
        // ...
    

    Note that this C++ is a little different from C in a couple of ways, one is that C requires the use of the enum keyword when declaring a variable:

    // day declaration in C:
    enum Days day = Saturday;
    
    0 讨论(0)
  • 2020-12-04 05:20

    If we want the strict type safety and scoped enum, using enum class is good in C++11.

    If we had to work in C++98, we can using the advice given by InitializeSahib,San to enable the scoped enum.

    If we also want the strict type safety, the follow code can implement somthing like enum.

    #include <iostream>
    class Color
    {
    public:
        static Color RED()
        {
            return Color(0);
        }
        static Color BLUE()
        {
            return Color(1);
        }
        bool operator==(const Color &rhs) const
        {
            return this->value == rhs.value;
        }
        bool operator!=(const Color &rhs) const
        {
            return !(*this == rhs);
        }
    
    private:
        explicit Color(int value_) : value(value_) {}
        int value;
    };
    
    int main()
    {
        Color color = Color::RED();
        if (color == Color::RED())
        {
            std::cout << "red" << std::endl;
        }
        return 0;
    }
    

    The code is modified from the class Month example in book Effective C++ 3rd: Item 18

    0 讨论(0)
  • 2020-12-04 05:21

    If you are still using C++03 and want to use enums, you should be using enums inside a namespace. Eg:

    namespace Daysofweek{
    enum Days {Saturday, Sunday, Tuesday,Wednesday, Thursday, Friday};
    }
    

    You can use the enum outside the namespace like,

    Daysofweek::Days day = Daysofweek::Saturday;
    
    if (day == Daysofweek::Saturday)
    {
        std::cout<<"Ok its Saturday";
    }
    
    0 讨论(0)
  • 2020-12-04 05:26

    Rather than using a bunch of if-statements, enums lend themselves well to switch statements

    I use some enum/switch combinations in the level builder I am building for my game.

    EDIT: Another thing, I see you want syntax similar to;

    if(day == Days.Saturday)
    etc
    

    You can do this in C++:

    if(day == Days::Saturday)
    etc
    

    Here is a very simple example:

    EnumAppState.h

    #ifndef ENUMAPPSTATE_H
    #define ENUMAPPSTATE_H
    enum eAppState
    {
        STARTUP,
        EDIT,
        ZONECREATION,
        SHUTDOWN,
        NOCHANGE
    };
    #endif
    

    Somefile.cpp

    #include "EnumAppState.h"
    eAppState state = eAppState::STARTUP;
    switch(state)
    {
    case STARTUP:
        //Do stuff
        break;
    case EDIT:
        //Do stuff
        break;
    case ZONECREATION:
        //Do stuff
        break;
    case SHUTDOWN:
        //Do stuff
        break;
    case NOCHANGE:
        //Do stuff
        break;
    }
    
    0 讨论(0)
提交回复
热议问题