Initialization of Object Instances

When the compiler processes a definition of a variable, it uses its type definition to allocate the required amount of memory, either from the heap (for static and extern variables or for dynamic variables) or from the stack (for local automatic variables).

This is true for simple variables, arrays, structures, and classes with member functions. If the code later assigns a value to a variable, the variable does not need initialization at its definition. If the algorithm uses the variable as an rvalue, it needs initial values for its data members.

Cylinder c1;                     // data members are not initialized
double vol = c1.getVolume();     // no, this is no good

This coding pattern, however, might be appropriate if some default ...

Get Core C++ A Software Engineering Approach 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.