C++11: Using List Initializers to Call Constructors

Image

Recall from Section 4.8 that C++11 now provides a uniform initialization syntax called list initializers that can be used to initialize any variable. Lines 11–13 of Fig. 9.6 can be written using list initializers as follows:

Time t2{ 2 }; // hour specified; minute and second defaultedTime t3{ 21, 34 }; // hour and minute specified; second defaultedTime t4{ 12, 25, 42 }; // hour, minute and second specified

or

Time t2 = { 2 }; // hour specified; minute and second defaultedTime t3 = { 21, 34 }; // hour and minute specified; second ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.