c中结构体的初始化的疑问
struct Test{ int t; int s;};
结构也可以这样初始化,
struct Test q = {1, 2};
但是肯定不能这样复制。只是c中这样初始化的原理是什么。感觉比较奇怪,之前也没有这样初始化过。
[解决办法]
这是一种比较方便的初始化方法,就是让你可以在定义的时候进行初始化。
至于原理,不知道你要什么原理,只是方便的原理而已。
[解决办法]
Aggregate(聚合)的初始化式
ISO C++ 2003 标准 $8.5.1/1
An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3).
ISO C++ 2003 标准 $8.5.1/2
When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace-enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate. [[Example:
struct A {
int x;
struct B {
int i;
int j;
} b;
} a = { 1, { 2, 3 } };