TRT
Sign in
mediumAdvancedconcept

What are the five special member functions, and when should you default or delete each one?

Covers the rule of five, the interaction between user-declared operations and implicit generation, and the use of =default and =delete to express intent.

TRT practice prompt — not a verified question from a named employer.

The Runtime Theory Team2 min read
Learn it firstThe Rule of Five and When to Default ItPro — members onlyCopy buffers deeply when duplication is wanted, move them cheaply on transfer, and express the choice with default and delete on MyVector and MyString.

A strong answer

A strong candidate frames What are the five special member functions, and when should you default or delete each one? around the exact memory layout, compiler semantics, and hardware execution model covered in The Rule of Five and When to Default It. Covers the rule of five, the interaction between user-declared operations and implicit generation, and the use of =default and =delete to express intent.

A minimal vector owns a heap block plus size and capacity. The destructor releases the block. That much looks complete, yet failure appears as soon as the vector is copied or returned by value. A shallow copy duplicates the pointer without duplicating the block, so two objects claim one allocation. Both destructors release the same address and the second release corrupts the heap. The trace above shows the safe pattern, with copy building a second block and move transferring ownership while leaving the source empty.

Think of the five operations as a lease agreement for a rental unit. The destructor ends the lease and returns the keys. Copy creates a second unit with the same contents. Move hands the keys to a new tenant and voids the old set. If any clause is missing, two tenants arrive with the same key or a unit is left locked with no key at all. The agreement works only when all clauses share one view of ownership.

In a production interview, always connect the specification rule to its concrete runtime consequence: state what lives on the stack versus the heap or static storage segment, how the compiler or operating system validates alignments, lifetimes, and error codes, and what happens under adversarial or boundary inputs (such as zero-length buffers, integer overflow, concurrent mutation, or partial I/O). Backing your explanation with a small trace of register/memory state transitions immediately separates senior systems engineers from candidates who only memorized syntax.

What the interviewer is listening for

  • Mastery of core invariant: List the five special operations and state when each one runs.
  • Mastery of core invariant: Apply =default and =delete to express copy and move intent.
  • Mastery of core invariant: Explain why a destructor forces review of the other four operations.
  • Hardware and ABI awareness: Explaining how sizes, alignments, calling conventions, or kernel boundaries behave on real 64-bit targets rather than relying on implementation-defined assumptions.
  • Defensive engineering: Naming the exact compiler flags (-Wall -Wextra -Werror), static analyzers, or runtime sanitizers (-fsanitize=address,undefined,thread) that verify correctness in CI.

Common mistakes

  • Explaining only surface syntax without tracing the underlying memory addresses, ownership lifetime, or kernel/user-space state transitions.
  • Assuming undefined behavior (such as signed overflow, uninitialized reads, strict-aliasing violations, or dangling pointers) will "just work" because it passed an unoptimized -O0 debug build.
  • Ignoring return codes, partial reads/writes, alignment padding, or cleanup paths when an allocation or syscall fails midway through a function.

This answer walks

Practice follow-ups

  1. 01How does declaring a move constructor suppress the implicit copy constructor?
  2. 02When does the rule of zero apply instead of the rule of five?

Not started

Sign in to save your learning progress.

Sign in to save