2

I have a project where lots of the objects hold state by maintaining simple boolean flags. There are lots of these, so I maintain them within a uint32_t and use bit masking. There are now so many flags to keep track of, I've created an abstraction for them (just a class wrapping the uint32_t) with set(), clear(), etc.

My question: What's a nice accurate, concise name for this class? What name could I give this class so that you'd have a reasonable idea what it was [for] knowing the name only?

Some ideas I had:

  • FlagBank
  • FlagArray
  • etc

Any ideas?

Thanks in advance!
Cheers,
-Chris

Chris Tonkinson
  • 13,823
  • 14
  • 58
  • 90

2 Answers2

10

The Standard has such a class template and it is called std::bitset<N> (N for the number of bits/flags). The actual object of this class could be named according its purpose then, like state or something.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
2

FlagBank would be fairly descriptive.

But I have one suggestion. Instead of using uint32_t and bit masking, it might be less C-like to use an STL vector instead. It uses a template specialization for the boolean case where only one bit per element is used for the storage. Very efficient and MUCH more object oriented.

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 1
    +1, but "less C-like" is not really a good reason IMHO. That's just incidental. `vector` (or even better, `bitset`) is better just because it's easier to work with -- instead of writing `flags &= ~(1 << 4)` to turn off flag #5, you can write `flags[4] = false`. – j_random_hacker May 28 '10 at 16:05