TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C++ Coding Standard

Rule:  OLC#002Checked automatically with code checker

Synopsis:Copy assignment operators shall be protected from doing destructive actions if an object is assigned to itself
Severity Level:1
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:

const CInfraClass& 
CInfraClass::operator=(const CInfraClass& rSource)

    if (&rSource != this)
        // No assignment to itself: perform assignment now.

    return *this;

Literature References:
Ellemtel Rule 28
ISC++ Rule 5.12