C++ 结构成员初始化

C++ 结构成员初始化,c++,struct,C++,Struct,在阅读DirectWrite源代码时,我遇到了以下结构: /// <summary> /// Line breakpoint characteristics of a character. /// </summary> struct DWRITE_LINE_BREAKPOINT { /// <summary> /// Breaking condition before the character. /// </summary>

在阅读DirectWrite源代码时,我遇到了以下结构:

/// <summary>
/// Line breakpoint characteristics of a character.
/// </summary>
struct DWRITE_LINE_BREAKPOINT
{
    /// <summary>
    /// Breaking condition before the character.
    /// </summary>
    UINT8 breakConditionBefore  : 2;

    /// <summary>
    /// Breaking condition after the character.
    /// </summary>
    UINT8 breakConditionAfter   : 2;

    /// <summary>
    /// The character is some form of whitespace, which may be meaningful
    /// for justification.
    /// </summary>
    UINT8 isWhitespace          : 1;

    /// <summary>
    /// The character is a soft hyphen, often used to indicate hyphenation
    /// points inside words.
    /// </summary>
    UINT8 isSoftHyphen          : 1;

    UINT8 padding               : 2;
};
//
///字符的行断点特征。
/// 
结构数据写入\行\断点
{
/// 
///字符前的中断条件。
/// 
UINT8:2;
/// 
///字符后的中断条件。
/// 
UINT8:2;
/// 
///字符是某种形式的空白,可能有意义
///为了辩护。
/// 
UINT8是空白:1;
/// 
///字符是软连字符,通常用于表示连字符
///单词内部的要点。
/// 
UINT8是指:1;
UINT8填充:2;
};
注意每个成员声明后的奇怪“:”。我假设它是成员变量的默认初始化值

我尝试搜索谷歌来确认,但不知道它的确切名称,我没有走多远(大多数结果都与默认初始化有关)


此技术的名称是什么?

:2
声明一个2位的成员。这称为位字段。由于声明的位总数加起来为8,因此所有位字段成员都是相邻的,其类型为
UINT8
,因此
struct DWRITE\u LINE\u断点的大小为单个字节

注意每一个后面的奇怪“:” 成员声明。我要去 假设这是默认的初始化 成员变量的值

这不是默认的初始化。这意味着
breakConditionBefore
只是
2
位整数,
isWhitespace
1
位整数。等等

DWRITE\u LINE\u断点
中,一个8位整数(即UINT8)被分成5个成员,其中3个是2位整数,2个成员是1位整数


阅读

否,它不是默认的初始化列表,而是位字段。请参阅。

嗯,它们是位字段

标准文档本身为您提供了一个示例

从1.7.5的C++记忆模型,

[示例:声明为

struct {
char a;
int b:5,
c:11,
:0,
d:8;
struct {int ee:8;} e;`
}
包含四个独立的内存位置:字段
a
和位字段
d
e.ee
都是独立的内存位置, 位字段
b
c
共同构成 第四个内存位置。不能同时修改位字段
b
c
,但可以同时修改
b
a
。 -结束示例]

它们只是“奇怪”,因为你不知道它们是什么。:)