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 parameterized constructor is a special class member function that accepts one or more arguments to initialize an object’s state. In C++, object creation is a sequential process: memory is first allocated (e.g., via stack frame setup or operator new), and the constructor is subsequently invoked on that already-allocated memory to initialize the member variables with caller-provided values.

Syntax and Implementation

A parameterized constructor shares the exact name of its class and has no return type. The standard practice in C++ is to initialize member variables using a member initializer list rather than assigning values within the constructor body. This approach directly constructs the members, avoiding a default-construction phase followed by an assignment operation.
class Vector3D {
private:
    double x;
    double y;
    double z;

public:
    // Parameterized constructor declaration
    Vector3D(double x_val, double y_val, double z_val);
};

// Out-of-line definition using a member initializer list
Vector3D::Vector3D(double x_val, double y_val, double z_val) 
    : x(x_val), y(y_val), z(z_val) {
    // Constructor body executes after the initializer list
}

Instantiation Mechanisms

C++ provides multiple syntactical approaches to invoke a parameterized constructor during object creation:
// 1. Direct Initialization
Vector3D v1(1.0, 2.0, 3.0);

// 2. Explicit Call
Vector3D v2 = Vector3D(4.0, 5.0, 6.0);

// 3. Direct-List-Initialization / Brace Initialization (C++11 and later)
// Prevents narrowing conversions
Vector3D v3{7.0, 8.0, 9.0};

// 4. Dynamic Allocation
Vector3D* v4 = new Vector3D(10.0, 11.0, 12.0);

Technical Characteristics

Suppression of the Implicit Default Constructor When a parameterized constructor is declared, the C++ compiler automatically suppresses the generation of the implicit default constructor. If the class requires both parameterized and default instantiation, the default constructor must be explicitly declared or defaulted.
class Matrix {
public:
    Matrix(int rows, int cols); // Suppresses default constructor
    Matrix() = default;         // Explicitly restores default constructor
};
Constructor Overloading A class can define multiple parameterized constructors. The compiler resolves which constructor to invoke based on the arity (number of arguments) and the data types of the arguments provided during instantiation.
class StringWrapper {
public:
    StringWrapper(const char* str);        // Overload 1
    StringWrapper(const char* str, int n); // Overload 2
};
Converting Constructors and the explicit Specifier By default, constructors can act as implicit conversion operators. A constructor callable with a single argument—whether it defines exactly one parameter, or defines multiple parameters where all but the first possess default arguments (e.g., MyClass(int a, int b = 0))—enables implicit conversion from the argument type to the class type. Furthermore, since C++11, constructors with multiple parameters also act as implicit converting constructors during copy-list-initialization. To strictly enforce direct initialization and prevent unintended implicit conversions, the explicit keyword must be applied to the constructor declaration.
class Identifier {
private:
    int id;
    int sub_id;
public:
    // Callable with one argument due to the default parameter.
    // 'explicit' prevents implicit conversions and copy-list-initialization.
    explicit Identifier(int id_val, int sub_val = 0) : id(id_val), sub_id(sub_val) {}
};

// Identifier obj1 = 42;      // Error: Implicit conversion blocked by 'explicit'
// Identifier obj2 = {42, 1}; // Error: Copy-list-initialization blocked by 'explicit'
Identifier obj3(42);          // Valid: Direct initialization
Identifier obj4{42, 1};       // Valid: Direct-list-initialization
Master C++ with Deep Grasping Methodology!Learn More