Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A default constructor in C++ is a constructor that can be invoked without any arguments. It is responsible for initializing an object’s state (its base classes and data members) and is either explicitly defined by the developer or implicitly generated by the compiler. A constructor qualifies as a default constructor in two scenarios:
  1. It is declared with no parameters.
  2. It is declared with parameters, but all parameters have default arguments.
class Widget {
public:
    // Scenario 1: No parameters
    Widget() {
        // Initialization logic
    }
};

class Point {
public:
    // Scenario 2: All parameters have default arguments
    Point(int x = 0, int y = 0) {
        // Initialization logic
    }
};

Implicitly-Declared Default Constructor

If a class definition contains absolutely no user-declared constructors, the C++ compiler automatically generates an implicitly-declared default constructor. This compiler-generated constructor is inline and public. The initialization behavior of the implicitly-generated default constructor depends on the instantiation context and the presence of C++11 Non-Static Data Member Initializers (NSDMIs):
  • Base classes and class-type members: Initialized by invoking their respective default constructors.
  • Members with NSDMIs: Initialized to the value specified in the class definition (e.g., int x = 5;).
  • Fundamental types without NSDMIs: Initialization depends on the context:
    • Default-initialization (e.g., ImplicitExample obj;): Fundamental types are left uninitialized, containing indeterminate values.
    • Value-initialization (e.g., ImplicitExample obj{};): Fundamental types are zero-initialized.
class ImplicitExample {
    // No constructors declared.
    // Compiler generates: inline public ImplicitExample() { }
    
    std::string name; // Default-initialized to empty string
    int id = 10;      // Initialized via NSDMI
    int score;        // Indeterminate if default-initialized, 0 if value-initialized
};

Implicitly Deleted Default Constructor

Under specific conditions, the compiler will declare the implicit default constructor as deleted (= delete), rendering the class un-instantiable without arguments. This occurs if the class contains:
  • A reference member without an NSDMI.
  • A const member of any type that lacks a user-provided default constructor, without an NSDMI.
  • A non-static data member or a base class that lacks an accessible or non-deleted default constructor.

The Suppression Rule

The compiler will only generate a default constructor if no other constructors exist. If you declare any constructor (such as a parameterized constructor or a copy constructor), the compiler suppresses the generation of the implicit default constructor.
class SuppressedExample {
public:
    SuppressedExample(int value) {} 
    // The implicit default constructor is NOT generated.
    // SuppressedExample obj; // Compilation error
};

Explicit Control (C++11 and later)

Modern C++ introduced the = default and = delete specifiers, providing explicit control over the compiler’s generation of the default constructor. = default Instructs the compiler to generate the default implementation. This is primarily used to restore the default constructor when its implicit generation has been suppressed by the presence of another user-declared constructor. Using = default instead of an empty constructor body {} preserves the default constructor’s triviality (provided the class meets the requirements for a trivial default constructor). A trivial default constructor allows the compiler to optimize object creation by performing no action during default-initialization, and by safely applying zero-initialization during value-initialization.
class DefaultedExample {
public:
    DefaultedExample(int value) {}     // Suppresses implicit generation
    DefaultedExample() = default;      // Explicitly restores compiler generation
};
= delete Explicitly instructs the compiler not to generate the default constructor, rendering it ill-formed to attempt an argument-less instantiation of the class.
class DeletedExample {
public:
    DeletedExample() = delete;         // Forbids default construction
};
Master C++ with Deep Grasping Methodology!Learn More