Constructors in C++: default, copy and move

Difficulty: Intermediate

Question

Explain default, copy and move constructors (and the corresponding assignment operators). When does the compiler generate them, and when is each called?

Answer

Constructors are where object lifetime begins, so being fluent here signals that you understand C++ object semantics rather than just syntax. Let's walk through the family.

The default constructor takes no arguments. If you write no constructors at all, the compiler generates one that default-initialises members. The moment you declare any constructor, the implicit default constructor disappears, and you can bring it back with '= default'. Members that are built-in types stay uninitialised unless you give them initialisers, which is a classic source of garbage values.

The copy constructor, T(const T&), creates a new object as a copy of an existing one. It is invoked when you initialise from another object (T b = a;), pass an argument by value, or return by value where elision does not apply. The compiler-generated version copies each member (memberwise), which for raw pointers means copying the address only, not the pointed-to data. That is why classes owning resources need a custom one.

The move constructor, T(T&&), steals resources from a temporary or an object explicitly marked with std::move, leaving the source valid but unspecified (usually empty). It is much cheaper than copying a big buffer: think of the difference between photocopying a 500-page book and simply handing over the book. It should be marked noexcept, otherwise containers such as vector will prefer copying during reallocation to preserve their strong exception guarantee.

Note the difference between construction and assignment. 'T b = a;' calls the copy constructor because b is being created. 'b = a;' calls copy assignment, T& operator=(const T&), because b already exists, so assignment must first clean up b's old resources, and must be safe under self-assignment. The same split applies to move assignment.

When does the compiler generate what? Declaring a destructor, copy operation, or move operation affects the others. If you declare a move constructor or move assignment, the copy operations are implicitly deleted. If you declare a copy operation or a destructor, move operations are not generated and the compiler falls back to copying. That is the practical reason behind the rule of five.

Two more useful facts: the member initialiser list initialises members directly and is mandatory for const members, reference members and base classes without default constructors, and single-argument constructors should usually be marked explicit to prevent surprising implicit conversions.

Code examples

Which constructor gets called

#include <iostream>
#include <utility>

struct Widget {
    Widget()                          { std::cout << "default\n"; }
    Widget(const Widget&)             { std::cout << "copy\n"; }
    Widget(Widget&&) noexcept         { std::cout << "move\n"; }
    Widget& operator=(const Widget&)  { std::cout << "copy-assign\n"; return *this; }
    Widget& operator=(Widget&&) noexcept { std::cout << "move-assign\n"; return *this; }
};

int main() {
    Widget a;                    // default
    Widget b = a;                // copy constructor (b is being created)
    Widget c = std::move(a);     // move constructor
    b = c;                       // copy assignment (b already exists)
    b = std::move(c);            // move assignment
}

Initialisation with '=' at declaration calls a constructor; assignment to an existing object calls the assignment operators.

Key points

Common follow-up questions

Interview tips

Concepts covered

constructors, copy constructor, move constructor, initializer list

More C++ & STL questions