【C++】bool a:1;这个怎么解释?

Bit field, 位域。


意思大概就是你虽然声明自己是个bool(8bit),但我可以只占1bit的空间。

#include<iostream>
using namespace std;

struct S1
{
    bool b1    :1;
    bool b11    ;1;
    bool b111   :1;
    unsigned a  :20;
    unsigned b   :10;
}

节省内存,bool变量只需要1bit,编译器会帮助做位运算以保证赋值和取值都是正确的,但不能再取得地址了。

以时间换空间




猜你喜欢

转载自blog.csdn.net/u013346007/article/details/80896025
今日推荐