Overloading the Assignment Operator

On several occasions, I told you that the object initialization and object assignment are different things in C++. When you are dealing with built-in data types, this distinction is often academic. Consider, for example, the following segment of the client code.

int v = 5; int u = v;                 // variable u is initialized

Compare this with the following segment of code.

int v = 5; int u; u = v;              // variable u is assigned

In the first example, variable u is initialized at definition. In the second example, variable u is assigned after definition. For built-in variables, the end result is the same. When these computational objects are objects of programmer-defined types that handle their own memory, the difference ...

Get Core C++ A Software Engineering Approach now with O’Reilly online learning.

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