Program flags

Hi, I want to have flags in my code.

I am trying to do it with enums, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
enum EStatesFlags
{
 NONE_FLAGS      = 0x0,	

        WHITE     = 0x1,	
        RED         = 0x2,	
        BLUE            = 0x4,	
        ORANGE          = 0x8,	
}

//Get Set Data
    bool IsSetStatesFlags(EStatesFlags eFlag) { return (m_eStateFlags & eFlag); }
    void SetStatesFlags(EStatesFlags eFlags) { m_eStateFlags |= eFlags; }
    void RemoveStatesFlags(EStatesFlags eFlags) { m_eStateFlags &= ~eFlags; }



But I'd like to be able to add few flags at the same time. For instance, something like "SetFlags(BLUE | RED)" but I don't know how could I do it.

Any idea? thanks!
use "int" in the functions instead of the enum. And while you are at it, you can use const int instead of the enum constants too. There is no sane and type-safe way in C++ to handle collections of flags.

1
2
3
4
  const static int WHITE = 1;
  const static int RED = 2;
  ...
  bool IsSetStatesFlags(int eFlag) { ... }

Last edited on
Topic archived. No new replies allowed.