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 ...

Get Beginning C++ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.