6.9 COPY INITIALIZATION

It is possible to declare and initialize the object in a slightly different form. See the declaration given below.

Period p2 = p1;

It declares a variable object p2 of class Period and initializes it with a copy of object p1. It goes without saying that p1 is already declared as an object of type Period. This form is called copy initialization. It appears to be a copy constructor. It is not so. Difference between the two is quite subtle. Actually, it involves overloaded assignment operator. It is worth noting that if we do not overload operator =, then the compiler provides a default overloaded = operator.

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.