|Synopsis:||Copy assignment operators shall be protected from doing destructive actions if an object is assigned to itself|
|Category:||Object Life Cycle|
A common error is assigning an object to itself (a = a). Normally, destructors for instances which are allocated on the heap are invoked before assignment takes place. If an object is assigned to itself, the values of the instance variables will be lost before they are assigned. This may well lead to strange run-time errors. If a = a is detected, the assigned object should not be changed.
An assignment operator is typically implemented as follows:
CInfraClass::operator=(const CInfraClass& rSource)
if (&rSource != this)
// No assignment to itself: perform assignment now.