0
0
CppConceptBeginner · 3 min read

What is Move Constructor in C++: Explanation and Example

A move constructor in C++ is a special constructor that transfers resources from one object to another instead of copying them. It helps improve performance by 'moving' data, like pointers, rather than duplicating it.
⚙️

How It Works

Imagine you have a box full of toys and you want to give it to a friend. Instead of making a copy of every toy (which takes time and space), you just hand over the whole box and empty your own. This is what a move constructor does in C++: it takes ownership of resources from one object and leaves the original empty or in a safe state.

When you create a new object using a move constructor, it steals the internal data (like pointers to memory) from the old object. This avoids the cost of copying all the data. The old object is left in a state where it no longer owns the data, so it won’t try to free it later.

This is especially useful for objects that manage dynamic memory or other resources, making programs faster and more efficient.

💻

Example

This example shows a simple class with a move constructor that transfers ownership of a dynamic array.

cpp
#include <iostream>
#include <utility> // for std::move

class Buffer {
    int* data;
    size_t size;

public:
    // Constructor
    Buffer(size_t s) : size(s), data(new int[s]) {
        std::cout << "Constructed Buffer of size " << size << "\n";
    }

    // Move constructor
    Buffer(Buffer&& other) noexcept : data(nullptr), size(0) {
        std::cout << "Move constructor called\n";
        // Take ownership of other's data
        data = other.data;
        size = other.size;
        // Leave other in safe state
        other.data = nullptr;
        other.size = 0;
    }

    // Destructor
    ~Buffer() {
        delete[] data;
        std::cout << "Destroyed Buffer of size " << size << "\n";
    }

    // Disable copy constructor for clarity
    Buffer(const Buffer&) = delete;

    size_t getSize() const { return size; }
};

int main() {
    Buffer buf1(5);            // Create buffer
    Buffer buf2(std::move(buf1)); // Move buf1 into buf2
    std::cout << "buf2 size: " << buf2.getSize() << "\n";
    std::cout << "buf1 size after move: " << buf1.getSize() << "\n";
    return 0;
}
Output
Constructed Buffer of size 5 Move constructor called buf2 size: 5 buf1 size after move: 0 Destroyed Buffer of size 0 Destroyed Buffer of size 5
🎯

When to Use

Use a move constructor when your class manages resources like dynamic memory, file handles, or network connections. It helps avoid expensive copying by transferring ownership instead.

For example, when returning large objects from functions or storing them in containers, move constructors make your program faster and use less memory.

Modern C++ libraries and standard containers use move constructors automatically when possible, so defining one in your classes improves compatibility and performance.

Key Points

  • A move constructor transfers resources from one object to another without copying.
  • It leaves the original object in a safe, empty state.
  • It improves performance by avoiding expensive deep copies.
  • Use it in classes that manage dynamic resources.
  • It is called when an object is initialized from an rvalue (temporary or std::move).

Key Takeaways

A move constructor transfers ownership of resources instead of copying them.
It improves performance by avoiding deep copies of data.
Use move constructors in classes managing dynamic memory or resources.
The original object is left empty but valid after moving.
Move constructors are called with rvalues or std::move.