Why Can't the Default Constructor Be Called with Empty Brackets?
Last Updated :
16 Apr, 2024
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;
}
OutputDefault constructor called.
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
How to Define the Default Constructor in C++? In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In t
2 min read
How to Create a Class with Constructors and Destructors in C++? In C++, a class is a user-defined data type encapsulating data members and member functions. The constructors and destructors are special member functions of a class used for initializing objects and cleaning up resources respectively. In this article, we will discuss how to create a class with cons
2 min read
How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived
3 min read
Problem with Single Argument Constructor in C++ and How to solve it In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes a conversion constructor because such a constructor allows automatic conversion to the class being constructed. Problem:Whenever there is a constructor with a single argument and there is a
8 min read
How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w
3 min read
How to Initialize a Vector with Default Values in C++? Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++.The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a sim
2 min read