上QQ阅读APP看书,第一时间看更新
Passing Initializer lists
You can pass an initializer list as a parameter if that list can be converted to the type of the parameter. For example:
struct point { int x; int y; };
void set_point(point pt);
int main()
{
point p;
p.x = 1; p.y = 1;
set_point(p);
set_point({ 1, 1 });
return 0;
}
This code defines a structure that has two members. In the main function, a new instance of point is created on the stack and it is initialized by accessing the members directly. The instance is then passed to a function that has a point parameter. Since the parameter of set_point is pass-by-value, the compiler creates a copy of the structure on the stack of the function. The second call of set_point does the same: the compiler will create a temporary point object on the stack of the function and initialize it with the values in the initializer list.