Check for self-assignment with operator=:

A common mistake when implementing an assignment is to forget to check for self-references (the this pointer being the same as the argument being assigned). Clients will not normally do this intentionally, but it can happen accidentally, and with aliasing you cannot always tell when it's going to happen. It can also happen to surrogates, where the surrogate being assigned points at the same master object as the target of the assignment.

To avoid self-assignment, do nothing if this and the assignment argument are the same; but, if the argument is a reference, take its address. Also, make sure you do things in the right order: for example, when reference counting, increment the count of the new master before decrementing the count of the
old master.

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker