关于冒号的简单C ++语法问题 [英] Simple C++ syntax question about colon

查看:115
本文介绍了关于冒号的简单C ++语法问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚看到一段代码片段,其中一段语法我从来没有见过。
bool start:1; 是什么意思?

I just saw a code snippet with a piece of syntax that I have never seen before. What does bool start : 1; mean? I found it inside a class definition in a header file.

推荐答案

struct record {
    char *name;
    int refcount : 4;
    unsigned dirty : 1;
};

这些是位字段;该数字给出了字段的精确大小(以位为单位)。 (有关详细信息,请参阅有关C的任何完整的书。)位字段可用于节省具有多个二进制标志或其他小字段的结构中的空间,并且还可用于尝试符合外部强加的存储布局。 (它们在后一任务中的成功通过位字段在一些机器上从左到右分配并且从右到左在其他机器上分配的事实而减轻)。

Those are bit-fields; the number gives the exact size of the field, in bits. (See any complete book on C for the details.) Bit-fields can be used to save space in structures having several binary flags or other small fields, and they can also be used in an attempt to conform to externally-imposed storage layouts. (Their success at the latter task is mitigated by the fact that bit-fields are assigned left-to-right on some machines and right-to-left on others).

请注意,用于以位指定字段大小的冒号表示法仅在结构(和联合)中有效;您不能使用此机制来指定任意变量的大小。

Note that the colon notation for specifying the size of a field in bits is only valid in structures (and in unions); you cannot use this mechanism to specify the size of arbitrary variables.


  • 参考文献:K& R1 Sec。 6.7 pp。136-8

  • K& R2 Sec。 6.9 pp。149-50

  • ISO Sec。 6.5.2.1

  • H& S Sec。 5.6.5 pp。136-8

  • References: K&R1 Sec. 6.7 pp. 136-8
  • K&R2 Sec. 6.9 pp. 149-50
  • ISO Sec. 6.5.2.1
  • H&S Sec. 5.6.5 pp. 136-8

这篇关于关于冒号的简单C ++语法问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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