D
dj
Perhaps this question should be in the standard c newsgroup, but i hope
somebody answers it here. Anyway, I came across this situation in an
otherwise c++ code.
I need a struct that works like some sort of a flag set. The flag mask
is a single long, while the meaning of individual flags is given by
static const members of the same struct:
struct flag_set {
long flags;
static const long FLAG1_BIT = 0x1;
static const long FLAG2_BIT = 0x2;
...
static const long FLAGN_BIT = 1 << N-1;
}
Of course the consts have meaningful names in my code, so i can check
for some condition like this:
flag_set fs;
....
if (fs & flag_set::FLAGX_BIT) {
do something;
}
I suppose this is one of many possible ways to approach the problem but
i am not sure if it is the best one as well. The storage should not be
the problem, even with many flag_sets floating around, because static
members don't use additional space (i am pretty sure about that).
However, i have not seen much code written this way and would like to
know if someone knows for a better way to do it, that is for some reason
why the upper solution is not good.
If nothing else, this is a question of proper programming style that i
still need to work on. All answers without "this is a stupid question"
in them are much appreciated.
somebody answers it here. Anyway, I came across this situation in an
otherwise c++ code.
I need a struct that works like some sort of a flag set. The flag mask
is a single long, while the meaning of individual flags is given by
static const members of the same struct:
struct flag_set {
long flags;
static const long FLAG1_BIT = 0x1;
static const long FLAG2_BIT = 0x2;
...
static const long FLAGN_BIT = 1 << N-1;
}
Of course the consts have meaningful names in my code, so i can check
for some condition like this:
flag_set fs;
....
if (fs & flag_set::FLAGX_BIT) {
do something;
}
I suppose this is one of many possible ways to approach the problem but
i am not sure if it is the best one as well. The storage should not be
the problem, even with many flag_sets floating around, because static
members don't use additional space (i am pretty sure about that).
However, i have not seen much code written this way and would like to
know if someone knows for a better way to do it, that is for some reason
why the upper solution is not good.
If nothing else, this is a question of proper programming style that i
still need to work on. All answers without "this is a stupid question"
in them are much appreciated.