4.12. Class Member Initialization

If a data member object has a default constructor (one with a void argument list or a signature with all default arguments), the compiler calls its constructor automatically when you create an object. In situations where this is not true or in cases where you need to pass arguments to a constructor, a special syntax called member initialization is available. Suppose, for example, a user program instantiates an Act object with a character string title, a Roman number, and a length (say, the number of ASCII characters in a short play).

Act play("theater", 1, 32056);            // Act object 

Here is the Act class with a member initialization list in its constructor:

 class Act { private: char title[80]; // character array ...

Get Navigating C++ and Object-Oriented Design 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.