Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) copy assignment operator

 

The copy assignment operator is an operator used in assignment and one of The Big Four. It differs from a constructor, because the copy assignment operator might have to clean up the previous (to-be-overwritten) instance its resources.

 

There are multiple idioms for overloading a copy assignment operator:

 

 

 

 

 

 

The classic way

 

struct MyClass
{
  //Default constructor
  MyClass(const int x = 0) : m_x(x) {}

  //Canonical form of copy assignment operator
  MyClass& operator=(const MyClass& other)
  {
    //Prevent self-assignment
    if (this != &other)
    {
      //Copy member variables
      m_x = other.m_x;
    }
    //Return *this by convention
    return *this;
  }

  ///Read the member variable
  int GetX() const { return m_x; }

  private:
  //Member variables
  int m_x;
};

 

 

 

 

 

The swap idiom

 

#include <algorithm>

struct MyClass
{
  //Default constructor
  MyClass(const int x = 0) : m_x(x) {}

  //Canonical form of copy assignment operator
  MyClass& operator=(const MyClass& other)
  {
    MyClass temp(other);
    Swap(temp);

    //Return *this by convention
    return *this;
  }

  ///Read the member variable
  int GetX() const { return m_x; }

  private:
  //Member variables
  int m_x;

  void Swap(MyClass& other)
  {
    std::swap(m_x,other.m_x);
  }
};

 

 

 

 

 

Complete example

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppCopyAssignmentOperator.pro

 

TEMPLATE = app
CONFIG += console
CONFIG -= qt
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 


struct MyClassClassic
{
  //Default constructor
  MyClassClassic(const int x = 0) : m_x(x) {}

  //Canonical form of copy assignment operator
  MyClassClassic& operator=(const MyClassClassic& other)
  {
    //Prevent self-assignment
    if (this != &other)
    {
      //Copy member variables
      m_x = other.m_x;
    }
    //Return *this by convention
    return *this;
  }

  ///Read the member variable
  int GetX() const { return m_x; }

  private:
  //Member variables
  int m_x;
};

#include <algorithm>

struct MyClassSwap
{
  //Default constructor
  MyClassSwap(const int x = 0) : m_x(x) {}

  //Canonical form of copy assignment operator
  MyClassSwap& operator=(const MyClassSwap& other)
  {
    MyClassSwap temp(other);
    Swap(temp);

    //Return *this by convention
    return *this;
  }

  ///Read the member variable
  int GetX() const { return m_x; }

  private:
  //Member variables
  int m_x;

  void Swap(MyClassSwap& other)
  {
    std::swap(m_x,other.m_x);
  }
};

#include <cassert>

int main()
{
  {
    MyClassClassic x(1);
    MyClassClassic y(2);
    assert(x.GetX() != y.GetX());
    x = y;
    assert(x.GetX() == y.GetX());
  }
  {
    MyClassSwap x(1);
    MyClassSwap y(2);
    assert(x.GetX() != y.GetX());
    x = y;
    assert(x.GetX() == y.GetX());
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml