# Constructors in C++: default, copy and move

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

Canonical: https://www.internhack.in/learn/interview/cpp-interview/cpp-constructors-copy-move
Last updated: 2026-11-28

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

- Declaring any constructor suppresses the implicit default constructor; use = default to restore it.
- Copy constructor: T(const T&); move constructor: T(T&&) noexcept.
- T b = a is construction; b = a is assignment.
- Declaring a move operation deletes the implicit copy operations; declaring copy or destructor suppresses implicit moves.
- Use the member initialiser list; it is required for const and reference members.

## Common follow-up questions

- Why should a move constructor be noexcept?
- What does explicit do on a constructor?
- In what order are members and base classes initialised?

## Interview tips

- Keep the distinction between 'T b = a' and 'b = a' ready; it is asked constantly.
- Mention noexcept on move operations to show awareness of vector reallocation behaviour.

## Concepts covered

constructors, copy constructor, move constructor, initializer list

Previous: [RAII: Resource Acquisition Is Initialization](https://www.internhack.in/learn/interview/cpp-interview/cpp-raii) · Next: [Shallow copy vs deep copy](https://www.internhack.in/learn/interview/cpp-interview/cpp-shallow-vs-deep-copy) · [All of C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)

## More C++ & STL questions

- [Rule of three, rule of five and rule of zero](https://www.internhack.in/learn/interview/cpp-interview/cpp-rule-of-three-five-zero)
- [Lvalues, rvalues and move semantics](https://www.internhack.in/learn/interview/cpp-interview/cpp-lvalue-rvalue-move-semantics)
- [Copy elision, RVO and NRVO](https://www.internhack.in/learn/interview/cpp-interview/cpp-copy-elision-rvo)
- [std::move vs std::forward and perfect forwarding](https://www.internhack.in/learn/interview/cpp-interview/cpp-std-move-vs-forward)
- [Virtual functions, vtable and vptr](https://www.internhack.in/learn/interview/cpp-interview/cpp-virtual-functions-vtable)
- [Pure virtual functions and abstract classes](https://www.internhack.in/learn/interview/cpp-interview/cpp-pure-virtual-abstract-classes)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [C++ & STL](https://www.internhack.in/learn/interview/cpp-interview)
