Explain C++ default constructor generation rules

Quick Overview

This question evaluates understanding of C++ special-member-function rules—specifically when the compiler implicitly declares a default constructor, when that implicitly-declared constructor is defined as deleted, and how user-declared copy/move constructors, copy/move assignments, destructors, and member/base types (const, reference, or non-default-constructible) affect generation. Commonly asked to assess mastery of language semantics and object-initialization pitfalls, it belongs to the Software Engineering Fundamentals domain and tests both conceptual understanding of compiler behavior and practical application in C++ code.

Explain C++ default constructor generation rules

Company: Trexquant

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Onsite

You are given a C++ class type `T`. 1. When will the compiler **implicitly declare** a default constructor for `T`? 2. When will the implicitly-declared default constructor be **defined as deleted** (i.e., `T t;` is ill-formed)? 3. How do user-declared special member functions (copy/move ctor, copy/move assignment, destructor) affect whether a default constructor is generated? 4. What changes if members/base classes have no default constructors, are references, or are `const`? Answer in terms of the C++ “special member functions” model (rule of 0/3/5) and include a few small examples of classes that: - get an implicit default constructor, - do not get one, - get one but it is deleted.

Quick Answer: This question evaluates understanding of C++ special-member-function rules—specifically when the compiler implicitly declares a default constructor, when that implicitly-declared constructor is defined as deleted, and how user-declared copy/move constructors, copy/move assignments, destructors, and member/base types (const, reference, or non-default-constructible) affect generation. Commonly asked to assess mastery of language semantics and object-initialization pitfalls, it belongs to the Software Engineering Fundamentals domain and tests both conceptual understanding of compiler behavior and practical application in C++ code.

|Home/Software Engineering Fundamentals/Trexquant
Trexquant logo
Trexquant
Sep 1, 2025, 12:00 AM
mediumSoftware EngineerOnsiteSoftware Engineering Fundamentals
5
0

You are given a C++ class type T.

  1. When will the compiler implicitly declare a default constructor for T ?
  2. When will the implicitly-declared default constructor be defined as deleted (i.e., T t; is ill-formed)?
  3. How do user-declared special member functions (copy/move ctor, copy/move assignment, destructor) affect whether a default constructor is generated?
  4. What changes if members/base classes have no default constructors, are references, or are const ?

Answer in terms of the C++ “special member functions” model (rule of 0/3/5) and include a few small examples of classes that:

  • get an implicit default constructor,
  • do not get one,
  • get one but it is deleted.
Loading comments...