assignment operators

Assignment Operators

What is “self assignment”?

Self assignment is when someone assigns an object to itself. For example,

#include "Fred.h"  // Defines class Fred

void userCode(Fred& x)
{
  x = x;           // Self-assignment
}

Obviously no one ever explicitly does a self assignment like the above, but since more than one pointer or reference can point to the same object (aliasing), it is possible to have self assignment without knowing it:

#include "Fred.h"  // Defines class Fred

void userCode(Fred& x, Fred& y)
{
  x = y;           // Could be self-assignment if &x == &y
}

int main()
{
  Fred z;
  userCode(z, z);
  // ...
}

This is only valid for copy assignment. Self-assignment is not valid for move assignment.

Why should I worry about “self assignment”?

If you don’t worry about self assignment, you’ll expose your users to some very subtle bugs that have very subtle and often disastrous symptoms. For example, the following class will cause a complete disaster in the case of self-assignment:

class Wilma { };

class Fred {
public:
  Fred()                : p_(new Wilma())      { }
  Fred(const Fred& f)   : p_(new Wilma(*f.p_)) { }
 ~Fred()                { delete p_; }
  Fred& operator= (const Fred& f)
    {
      // Bad code: Doesn't handle self-assignment!
      delete p_;                // Line #1
      p_ = new Wilma(*f.p_);    // Line #2
      return *this;
    }
private:
  Wilma* p_;
};

If someone assigns a Fred object to itself, line #1 deletes both this->p_ and f.p_ since *this and f are the same object. But line #2 uses *f.p_, which is no longer a valid object. This will likely cause a major disaster.

The bottom line is that you the author of class Fred are responsible to make sure self-assignment on a Fred object is innocuous. Do not assume that users won’t ever do that to your objects. It is your fault if your object crashes when it gets a self-assignment.

Aside: the above Fred::operator= (const Fred&) has a second problem: If an exception is thrown while evaluating new Wilma(*f.p_) (e.g., an out-of-memory exception or an exception in Wilma’s copy constructor), this->p_ will be a dangling pointer — it will point to memory that is no longer valid. This can be solved by allocating the new objects before deleting the old objects.

This is only valid for copy assignment. Self-assignment is not valid for move assignment.

Okay, okay, already; I’ll handle self-assignment. How do I do it?

You should worry about self assignment every time you create a class. This does not mean that you need to add extra code to all your classes: as long as your objects gracefully handle self assignment, it doesn’t matter whether you had to add extra code or not.

We will illustrate the two cases using the assignment operator in the previous FAQ:

  1. If self-assignment can be handled without any extra code, don’t add any extra code. But do add a comment so others will know that your assignment operator gracefully handles self-assignment:

    Example 1a:

    Fred& Fred::operator= (const Fred& f)
    {
      // This gracefully handles self assignment
      *p_ = *f.p_;
      return *this;
    }
    

    Example 1b:

    Fred& Fred::operator= (const Fred& f)
    {
      // This gracefully handles self assignment
      Wilma* tmp = new Wilma(*f.p_);   // No corruption if this line threw an exception
      delete p_;
      p_ = tmp;
      return *this;
    }
    
  2. If you need to add extra code to your assignment operator, here’s a simple and effective technique:

    Fred& Fred::operator= (const Fred& f)
    {
      if (this == &f) return *this;   // Gracefully handle self assignment
      // Put the normal assignment duties here...
      return *this;
    }
    

    Or equivalently:

    Fred& Fred::operator= (const Fred& f)
    {
      if (this != &f) {   // Gracefully handle self assignment
        // Put the normal assignment duties here...
      }
      return *this;
    }
    

By the way: the goal is not to make self-assignment fast. If you don’t need to explicitly test for self-assignment, for example, if your code works correctly (even if slowly) in the case of self-assignment, then do not put an if test in your assignment operator just to make the self-assignment case fast. The reason is simple: self-assignment is almost always rare, so it merely needs to be correct - it does not need to be efficient. Adding the unnecessary if statement would make a rare case faster by adding an extra conditional-branch to the normal case, punishing the many to benefit the few.

In this case, however, you should add a comment at the top of your assignment operator indicating that the rest of the code makes self-assignment is benign, and that is why you didn’t explicitly test for it. That way future maintainers will know to make sure self-assignment stays benign, or if not, they will need to add the if test.

This is only valid for copy assignment. Self-assignment is not valid for move assignment.

I’m creating a derived class; should my assignment operators call my base class’s assignment operators?

Yes (if you need to define assignment operators in the first place).

If you define your own assignment operators, the compiler will not automatically call your base class’s assignment operators for you. Unless your base class’s assignment operators themselves are broken, you should call them explicitly from your derived class’s assignment operators (again, assuming you create them in the first place).

However if you do not create your own assignment operators, the ones that the compiler create for you will automatically call your base class’s assignment operators.

Example:

class Base {
  // ...
};

class Derived : public Base {
public:
  // ...
  Derived& operator= (const Derived& d);
  Derived& operator= (Derived&& d);
  // ...
};

Derived& Derived::operator= (const Derived& d)
{
  // Make sure self-assignment is benign
  Base::operator= (d);

  // Do the rest of your assignment operator here...
  return *this;
}

Derived& Derived::operator= (Derived&& d)
{
  // self-assignment is not allowed in move assignment
  Base::operator= (std::move(d));

  // Do the rest of your assignment operator here...
  return *this;
}