Equality Now!

In the case of the string class, as in most cases, the only reasons why we have to worry about this are to be able to determine whether two objects that are referred to by different names are actually the same object and to return a reference to the left-hand object in an assignment statement. You can see both uses of this in the code for string::operator = (const string& Str). The first use is illustrated by the following line:

    if (&Str != this)

Most of this statement should be familiar by now. Like other if statements, it tests a condition for truth or falsity. The != is the comparison operator that tests for "not equal", and we've just seen that this is the address of the variable for which the operator = function was called; ...

Get Learning to Program in C++ 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.