Open In App

Why Can’t the Default Constructor Be Called with Empty Brackets?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, the default constructor is a constructor that has no arguments or default value for all the arguments. If we don’t explicitly define a default constructor, the compiler generates a default constructor during compilation. In this article, we will learn why can’t the default constructor be called with empty brackets in C++.

Problem of Calling Default Constructor with Empty Brackets

In C++, the reason we can’t call the default constructor with empty brackets is due to the language’s interpretation of function declarations. According to the C++ standard, anything that can be interpreted as a function declaration will indeed be interpreted as such. Therefore, when we use empty brackets, it signifies a function call without any arguments.

When we define a constructor with parameters, it becomes the only constructor for that class, and the compiler no longer generates the default constructor (without parameters). So, if we try to call the default constructor with empty brackets, the compiler interprets it as a declaration of a function that takes no arguments. It will then look for a function that matches that signature (no parameters), and if it’s not there, it will throw an error.

For example, if we attempt to invoke the default constructor MyClass obj() with empty brackets in the below example, the code won’t compile and the program will throw an error because the default constructor hasn’t been explicitly defined due to the presence of the parameterized constructor.

C++
// C++ program to attempt calling default constructor with
// empty bracket

#include <iostream>
using namespace std;

class MyClass {
public:
    // Constructor with parameters
    MyClass(int value)
    {
        cout << "Constructor with parameter called. Value: "
             << value << endl;
    }
};

int main()
{
    // Attempt to call the default constructor with empty
    // brackets
    MyClass
    obj(); // This line will result in a compilation error

    return 0;
}

Output

warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]

Calling Default Constructor with Empty Brackets

To call a default constructor with empty brackets, we can explicitly define a constructor with no parameters, which will serve as the default constructor and then we can call the default constructor with empty brackets.

C++ Program to Call Default Constructor with Empty Brackets

The following program illustrates how we can call a default constructor with empty brackets in C++.

C++
// C++ Program to Call Default Constructor with Empty
// Brackets
#include <iostream>
using namespace std;

class MyClass {
public:
    // Default constructor with empty brackets
    MyClass()
    {
        cout << "Default constructor called." << endl;
    }

    // Constructor with parameters
    MyClass(int value)
    {
        cout << "Constructor with parameter called. Value: "
             << value << endl;
    }
};

int main()
{
    // Call the default constructor
    // Declare an instance of the class and the default
    // consttrcutor will be called automatically
    MyClass obj;

    return 0;
}

Output
Default constructor called.

Time Complexity: O(1)
Auxiliary Space: O(1)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads