Jump to content
  • Advertisement
Sign in to follow this  
fpsgamer

[C++] Bitflags and enumerations

This topic is 4009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This question has more to do with style than anything else.... I have seen bit flags declared in an enumeration, however this seems pointless to me. For example, please consider the following: enum FLAGS { RED = 0x01, BLUE = 0x02, GREEN = 0x04 ... etc}; As soon as you perform any binary op (like OR for example), the type is no longer 'FLAGS', so you're not making use of having typed integer constants. FLAGS f = RED | GREEN; // ILLEGAL! Using an UN-typed enumeration is also a possibility like so: enum { RED = 0x01, BLUE = 0x02, GREEN = 0x04 ... etc}; // No type However at this point you aren't using the second feature of enumerations: the ability to automatically assign incremented values to your constants (which we don't even want anyways). It seems to me that these constants might as well have been written as such: uint8_t RED = 0x01; uint8_t GREEN = 0x02; uint8_t BLUE = 0x04; ... Stylistically what is preferred? Also, is it even possible to create a set of type'd bit flags that can be combined?

Share this post


Link to post
Share on other sites
Advertisement
Lots of people do the enum thing and then just pass them around as int's. Other people do the enum thing and then use operator overloading to make the bitwise operators work the way you would expect.

I prefer to skip the enum thing and just make them const int's.

There really isn't a "preferred" style as such. People really wish the enum thing would work, it doesn't, so they either try to force it or do something else depending on how they feel.

FWIW, in C# the enum thing has first class support.

Share this post


Link to post
Share on other sites
I usually use const whatever's inside a namespace.

It's a pity the enum system doesn't work better than it does in C++, but that's how it is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!