6.12 END OF CHAPTER PROGRAMS

6.12.1 Initializer list

C++ aims at making user-defined types as easy to use as built-in-types. In particular, user-defined types may be declared, and their values initialized, the same way as built in types. Therefore, we can use initializer list for initializing the object.

Consider a class Point defined as shown below.

class Point  { public :      int x, y  }

An object of this class can be initialized as

Point p1 = { 3, 4 };

It is somewhat similar to the initialization of a structure.

However, this technique cannot be used for object belonging to any class. The restrictions on the class are as follows:

  • The class should not have private or protected non-static variables.
  • It should not contain any virtual methods. ...

Get Object Oriented Programming with C++, Second Edition 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.