O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Overloading Assignment and the Copy Constructor

There is one area where nothing does the job better than operator overloading. You have seen the copy constructor and how it is used. As mentioned, it is typically only needed when member variables point to the heap. It is also only invoked when an object is explicitly or implicitly created as part of a copy (for instance, when a local variable is returned from a member function).

Assignment is not the same as copy creation—assignment can occur at any point in the life span of an object, not just at the beginning. For instance:

aRequest Request1(aRequest::add,34);
aRequest Request2(aRequest::multiply,22);
Request2 = Request1;

After the last statement, you expect Request2 to have an Operator() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required