关于在c ++中创建标志结构的问题 [英] Question about creating a struct of flags in c++

查看:52
本文介绍了关于在c ++中创建标志结构的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种有效的方法在C ++中创建一个flag结构?

我需要创建一个boolean标志的结构,如下所示:

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;

};


但是,如果我这样做,我打印出sizeof(),该结构,它是13.

所以我认为每个标志的编译使用1个字节。


是否可以创建一个结构,以便每个标志只使用1位。


谢谢你。

解决方案

Pl **** **** @gmail.com 写道:


是否有一种在C ++中创建标志结构的有效方法?


我需要创建一个boolean标志的结构,如下所示:

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;

};


但如果我这样做,我打印出来sizeof(),那个结构,它是13.

所以我认为编译每个标志使用1个字节。


是否可以创建结构这样每个标志只使用1位。



是的,请阅读有关位域的信息。语法是冒号和字段宽度

在成员名称之后,例如


bool flag1:1;


V

-

请在通过电子邮件回复时删除资金''A'

我不回复请不要问回复,请不要问


10月29日晚上11点10分,Victor Bazarov< v.Abaza ... @ comAcast。 netwrote:


Plisske ... @ gmail.com写道:


有没有一种有效的方法在C ++中创建一个flag结构?


我需要创建一个boolean标志的结构,如下所示:

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;

};


但是如果我这样做,我打印出sizeof(),那个结构,它是13.

所以我认为编译使用每个标志1个字节。


是否可以创建一个结构,以便每个标志仅使用1位。



是的,请阅读有关位域的信息。 *语法是冒号和字段宽度

在成员名称之后,如


* * * bool flag1:1;


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问



谢谢Victor。


我是要按照你的建议。如果我创建一个这样的类:

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;

};


我可以通过这样做将所有标志设置为0:


myMask mask;


memset(& mask,''\''',sizeof(myMask));


我可以比较2个面具是否是这样做:


myMask mask1;

myMask mask2;


memcmp(& mask1,& ; mask2,sizeof(myMask));


谢谢。


On 10 ??30è?,????1ê ±50·?,Plisske ... @ gmail.com < Plisske ... @ gmail.com>

写道:


10月29日晚上11点10分,Victor Bazarov< v.Abaza ... @ comAcast.netwrote:



Plisske ... @ gmail.com写道:
< blockquote class =post_quotes>
有没有一种在C ++中创建flag结构的有效方法?


我需要创建一个boolean标志的结构,如下所示:

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;

};


但是如果我这样做,我打印出sizeof(),那个结构和它是13.

所以我认为编译使用每个标志1个字节。


是否可以创建一个结构,以便每个标志仅使用1位。


是的,请阅读有关位域的信息。语法是冒号和字段宽度

在成员名称之后,如


bool flag1:1;


V

-

请在回复时删除资金''A'通过电子邮件

我没有回复最热门的回复,请不要问



谢谢Victor。


我会按照你的建议。如果我创建一个这样的类:

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;

};


我可以通过这样做将所有标志设置为0:


myMask mask;


memset(& mask,''\''',sizeof(myMask));


我可以比较2个面具是否是这样做:


myMask mask1;

myMask mask2;


memcmp(& mask1,& ; mask2,sizeof(myMask));


谢谢.-òt2?±?òyó???×? -


- ??ê?òyó?μ???×? -



有两件事要说:

1,你不需要类,但是struct,因为struct足够好了/>
解决它;

2,无论是类还是结构,你可以使用ctor,其中所有位字段

都设置为0或任何你想要的值。


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.

Thank you.

解决方案

Pl********@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


On Oct 29, 11:10*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:

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.


On 10??30è?, ????1ê±50·?, "Plisske...@gmail.com" <Plisske...@gmail.com>
wrote:

On Oct 29, 11:10 pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:


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.- òt2?±?òyó???×? -

- ??ê?òyó?μ???×? -

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.


这篇关于关于在c ++中创建标志结构的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆