结构(C++)

struct 关键字定义一个结构类型和结构类型的变量。

[template-spec] struct[ms-decl-spec] [tag [: base-list ]]
{ 
   member-list 
} [declarators];
[struct] tag declarators;

参数

  • template-spec
    选项模板规范。 有关更多信息,请参见 模板规范

  • struct
    struct关键字。

  • ms-decl-spec
    选项存储类的规范。 更多信息,请参见 __declspec 关键字。

  • tag
    类型名称命名结构。 在结构的范围内,标记将变成保留字。 标记是可选的。 如果省略,匿名结构定义。 有关更多信息,请参见 匿名类类型

  • base-list
    选项列表类或结构此结构将派生其成员距离。 有关更多信息,请参见基类。 每个基类或结构名称可以在访问说明符 (公共专用保护) 和 虚拟 关键字之后。 在参见 对类成员的控件访问 的成员访问表有关更多信息。

  • member-list
    列表结构成员。 有关更多信息,请参考 选件类成员。 此处唯一的区别是 struct 在 class位置。

  • declarators
    声明中指定类的名称。 公告列表声明结构类型的一个或多个实例。 ,如果类的所有数据成员是 public,声明可以包含初始值设定项列表。 默认情况下,,因为数据成员是 public 初始值设定项列表是通用的。结构。 有关更多信息,请参见声明概述

备注

结构类型是用户定义的聚合类型。 它可能具有不同的类型由字段或成员组成。

在 C++ 中,结构相同。类默认情况下,只不过其成员是 public 。

有关托管类和结构的信息,请参见 类和结构

使用结构

在 C 中,必须显式使用 struct 关键字声明结构。 在 C++ 中,,在类型定义,这不是必需的。

有声明变量的选项,则 framework 类型通过将一个或多个逗号分隔的变量名定义时在右大括号和分号之间。

结构变量进行初始化。 在大括号必须将每个变量的初始化。

有关相关信息,请参见 联合枚举

示例

// struct1.cpp
struct PERSON {   // Declare PERSON struct type
   int age;   // Declare member types
   long ss;
   float weight;
   char name[25];
} family_member;   // Define object of type PERSON

int main() {
   struct PERSON sister;   // C style structure declaration
   PERSON brother;   // C++ style structure declaration
   sister.age = 13;   // assign values to members
   brother.age = 7;
}

struct POINT {   // Declare POINT structure
   int x;   // Define members x and y
   int y;
} spot = { 20, 40 };    // Variable spot has
                        // values x = 20, y = 40

struct POINT there;     // Variable there has POINT type

struct CELL {   // Declare CELL bit field
   unsigned short character  : 8;  // 00000000 ????????
   unsigned short foreground : 3;  // 00000??? 00000000
   unsigned short intensity  : 1;  // 0000?000 00000000
   unsigned short background : 3;  // 0???0000 00000000
   unsigned short blink      : 1;  // ?0000000 00000000
} screen[25][80];       // Array of bit fields 

请参见

参考

定义选件类类型