How to use C++11 enum class for flags

后端 未结 5 1286
面向向阳花
面向向阳花 2020-12-06 17:52

Say I have such a class:

enum class Flags : char
{
    FLAG_1 = 1;
    FLAG_2 = 2;
    FLAG_3 = 4;
    FLAG_4 = 8;
};

Now can I have a vari

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-06 18:07

    At this point, It probably makes sense to define your own class to handle this.

     /** Warning: Untested code **/
     struct Flag {
    
         static Flag Flag_1;
         static Flag Flag_2;
         static Flag Flag_3;
         static Flag Flag_4;
    
         Flag operator = (Flag);
     private:
         char const value;
     };
    
     Flag operator | (Flag, Flag); 
    

提交回复
热议问题