Accessing to enum values by '::' in C++

后端 未结 3 1250
情深已故
情深已故 2021-02-13 09:35

I have class like following:

class Car  
{  
public:  
    Car();  
    // Some functions and members and enums  
    enum Color
    {
              


        
3条回答
  •  礼貌的吻别
    2021-02-13 10:17

    When I want to do something like this I tend to use a namespace and a typedef outside of th namespace (though usually I'm doing this globally rather than inside a class). Something like this:

    namespace colors 
    {
        enum Color 
        {
            Red,
            Blue
            ...
        }
    }
    typedef colors::Color Color;
    

    This way you use the namespace to get at the actual colors, but the Color type itself is still globally accessible:

    Color myFav = colors::Red;
    

提交回复
热议问题