Chris posted:
> Is there ever a reason to declare this as
>
> if(*this == rhs)
That is not a declaration -- choose your words carefully.
> as opposed to what I normally see
>
> if(this == &rhs)
The latter compares the ADDRESSES of two objects, while the former compares
the two objects themselves.
> Seems like the former version is going to be more expensive rather than
> simply comparing addresses as in the latter (plus the former requires
> the class to define operator== as well, no?).
Yes, the former requires an accessible operator==.
> Wondering if that added effort is ever justified.
Depends on a wonderous amount of factors:
(1) How the class is implemented
(2) How expensive it is to copy it
(3) How expensive it is to compare it for equality
(4) How often an object is compared to itself
If an object of the class should NEVER be assigned to itself, I would
suggest an assert:
#include <cassert>
MyClass &MyClass:

perator=(MyClass const &rhs)
{
assert(this != &rhs);
/* Now perform assignment */
return *this;
}
--
Frederick Gotham