聚合类

来源:互联网 发布:别人要我电脑mac地址 编辑:程序博客网 时间:2024/05/18 01:47
1、聚合类是一种没有用户定义的构造函数,没有私有(private)和保护(protected)非静态数据成员,没有基类,没有虚函数。这样的类可以由封闭的大括号用逗号分隔开初始化列表。下列的代码在 C 和 C++ 具有相同的语法:
struct C
{
int a;
double b;
};

struct D
{
int a; 
double b;
C c;
};

// initialize an object of type C with an initializer-list
C c = { 1, 2 };

// D has a sub-aggregate of type C. In such cases initializer-clauses can be nested
D d = { 10, 20, { 1, 2 } };

如果一个类里面包含了用户自定义的构造函数,而又用{ xx, xx, ...}来初始化它的对象,编译器就会报错

vc —— error C2552: "xx" 不能用初始值设定项列表初始化非聚合

gcc—— error: xx must be initialized by constructor, not by '{...}'

2、聚合定义为:

数组

没有以下内容的类、结构和联合:

构造函数

私有或受保护的成员

基类

虚函数
0 0
原创粉丝点击