Hisense
10/30/2008 8:07:00 AM
On 10?30?, ??1?50?, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:
> On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
>
>
>
>
>
> > Plisske...@gmail.com wrote:
> > > Is there an efficient way to create a struct of flag in C++?
>
> > > I need to create a struct of boolean flag, like this:
> > > struct testStruct {
> > > bool flag1;
> > > bool flag2;
> > > bool flag3;
> > > bool flag4;
> > > bool flag5;
> > > bool flag6;
> > > bool flag7;
> > > bool flag8;
> > > bool flag9;
> > > bool flag10;
> > > bool flag11;
> > > bool flag12;
> > > bool flag13;
> > > };
>
> > > But if I do that, i print out the sizeof(), that struct and it is 13.
> > > So i think the compile use 1 byte for each flag.
>
> > > Is it possible to create a struct so that each flag uses only 1 bit.
>
> > Yes, read about bitfields. The syntax is the colon and the field width
> > after the name of the member, like
>
> > bool flag1:1;
>
> > V
> > --
> > Please remove capital 'A's when replying by e-mail
> > I do not respond to top-posted replies, please don't ask
>
> Thank you Victor.
>
> I am going to follow you suggestion. If I create a class like this:
> class myMask {
> public:
> bool flag1:1;
> bool flag2:1;
> bool flag3:1;
> bool flag4:1;
> bool flag5:1;
> bool flag6:1;
> bool flag7:1;
> bool flag8:1;
> bool flag9:1;
> bool flag10:1;
> bool flag11:1;
> bool flag12:1;
> bool flag13:1;
> };
>
> Can I set all the flag to 0 by doing this:
>
> myMask mask;
>
> memset(&mask, '\0', sizeof(myMask));
>
> And can I compare if 2 masks are the same by doing this:
>
> myMask mask1;
> myMask mask2;
>
> memcmp(&mask1, &mask2, sizeof(myMask));
>
> Thanks.- ??????? -
>
> - ??????? -
there are two things to say:
1, you need no class, but struct, because struct is good enough to
solve it;
2, either class or struct, you could use ctor where all the bit fields
are set to 0 or any value you want.