Move constructor

suggest change

Say we have this code snippet.

class A {
    int a;
    int b;
    A(const A &other) {
        this->a = other.a;
        this->b = other.b;

To create a copy constructor, that is, to make a function that copies an object and creates a new one, we normally would choose the syntax shown above, we would have a constructor for A that takes an reference to another object of type A, and we would copy the object manually inside the method.

Alternatively, we could have written A(const A &) = default; which automatically copies over all members, making use of its copy constructor.

To create a move constructor, however, we will be taking an rvalue reference instead of an lvalue reference, like here.

class Wallet {
    int nrOfDollars;
    Wallet() = default; //default ctor

    Wallet(Wallet &&other) {
        this->nrOfDollars = other.nrOfDollars;
        other.nrOfDollars = 0;

Please notice that we set the old values to zero. The default move constructor (Wallet(Wallet&&) = default;) copies the value of nrOfDollars, as it is a POD.

As move semantics are designed to allow ‘stealing’ state from the original instance, it is important to consider how the original instance should look like after this stealing. In this case, if we would not change the value to zero we would have doubled the amount of dollars into play.

Wallet a;
a.nrOfDollars = 1;
Wallet b (std::move(a)); //calling B(B&& other);
std::cout << a.nrOfDollars << std::endl; //0
std::cout << b.nrOfDollars << std::endl; //1

Thus we have move constructed an object from an old one.

While the above is a simple example, it shows what the move constructor is intended to do. It becomes more useful in more complex cases, such as when resource management is involved.

// Manages operations involving a specified type.
// Owns a helper on the heap, and one in its memory (presumably on the stack).
// Both helpers are DefaultConstructible, CopyConstructible, and MoveConstructible.
template<typename T,
         template<typename> typename HeapHelper,
         template<typename> typename StackHelper>
class OperationsManager {
    using MyType = OperationsManager<T, HeapHelper, StackHelper>;

    HeapHelper<T>* h_helper;
    StackHelper<T> s_helper;
    // ...

    // Default constructor & Rule of Five.
    OperationsManager() : h_helper(new HeapHelper<T>) {}
    OperationsManager(const MyType& other)
      : h_helper(new HeapHelper<T>(*other.h_helper)), s_helper(other.s_helper) {}
    MyType& operator=(MyType copy) {
        swap(*this, copy);
        return *this;
    ~OperationsManager() {
        if (h_helper) { delete h_helper; }

    // Move constructor (without swap()).
    // Takes other's HeapHelper<T>*.
    // Takes other's StackHelper<T>, by forcing the use of StackHelper<T>'s move constructor.
    // Replaces other's HeapHelper<T>* with nullptr, to keep other from deleting our shiny
    //  new helper when it's destroyed.
    OperationsManager(MyType&& other) noexcept
      : h_helper(other.h_helper),
        s_helper(std::move(other.s_helper)) {
        other.h_helper = nullptr;

    // Move constructor (with swap()).
    // Places our members in the condition we want other's to be in, then switches members
    //  with other.
    // OperationsManager(MyType&& other) noexcept : h_helper(nullptr) {
    //     swap(*this, other);
    // }

    // Copy/move helper.
    friend void swap(MyType& left, MyType& right) noexcept {
        std::swap(left.h_helper, right.h_helper);
        std::swap(left.s_helper, right.s_helper);

Feedback about page:

Optional: your email if you want me to get back to you:

Table Of Contents