class 成员声明后的冒号?
Colon after class member declaration?
最近遇到一些很奇怪的语法:
struct Test {
size_t a : 2;
size_t b : 3;
size_t c : 4;
};
奇怪的是,它使用 GCC 4.9.2 编译,所有警告标志都打开。
void test_test() {
Test test;
std::cout << test.a << " " << test.b << " " << test.c << std::endl;
}
虽然声明测试没有错误并输出 0 0 0
(我相信 0 只是巧合;因为该结构是普通旧数据,其成员的 none 默认初始化为 0),更改
通过 Test test();
对定义的声明给出了错误
tester.cpp:14:20: error: request for member 'a' in 'test', which is of non-class
type 'Test()'
启用 C++11 会删除错误消息,但值仍然神秘地保持为 0。此语法实现了什么?
此语法是位域。
struct Test {
size_t a : 2; // Occupies two bits
size_t b : 3; // Occupies three bits
size_t c : 4; // Occupies four bits
};
最近遇到一些很奇怪的语法:
struct Test {
size_t a : 2;
size_t b : 3;
size_t c : 4;
};
奇怪的是,它使用 GCC 4.9.2 编译,所有警告标志都打开。
void test_test() {
Test test;
std::cout << test.a << " " << test.b << " " << test.c << std::endl;
}
虽然声明测试没有错误并输出 0 0 0
(我相信 0 只是巧合;因为该结构是普通旧数据,其成员的 none 默认初始化为 0),更改
通过 Test test();
对定义的声明给出了错误
tester.cpp:14:20: error: request for member 'a' in 'test', which is of non-class
type 'Test()'
启用 C++11 会删除错误消息,但值仍然神秘地保持为 0。此语法实现了什么?
此语法是位域。
struct Test {
size_t a : 2; // Occupies two bits
size_t b : 3; // Occupies three bits
size_t c : 4; // Occupies four bits
};