Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why default argument constructor is called as default constructor

Class A {
public:
       A(int i = 0, int k = 0) {} // default constructor WHY ??
       ~A() {}
};
int main()
{
  A a; // This creates object using defined default 
       // constructor but the constructor still has two arguments
  A b(1,2); // Called as parametrized one
}

Why this default argument constructor is default constructor. Why it is not called Parametrized constructor or default parametrized constructor because even if this constructor is called with no arguments it does contain two arguments ?? Is there any specific reason or its just because the standard says so.

like image 629
Tejendra Avatar asked Apr 16 '14 11:04

Tejendra


People also ask

Why is default constructor being called?

When a class constructor does not explicitly call the constructor of one of its object-valued fields in its initializer list, the default constructor for the field's class is called. In the standard library, certain containers "fill in" values using the default constructor when the value is not given explicitly.

Why a compiler given constructor is called as default constructor?

A default constructor is a constructor created by the compiler if we do not define any constructor(s) for a class. Here is an example: public class Student { String firstName; String lastName; int age; public static void main(String args[]) { Student myStudent = new Student(); myStudent.

What is the difference between default constructor and default argument constructor?

A default constructor is a 0 argument constructor which contains a no-argument call to the super class constructor. To assign default values to the newly created objects is the main responsibility of default constructor.

What is default constructor argument?

A default constructor is a constructor which can be called with no arguments (either defined with an empty parameter list, or with default arguments provided for every parameter). A type with a public default constructor is DefaultConstructible.


3 Answers

C++11 §12.1 Constructors

A default constructor for a class X is a constructor of class X that can be called without an argument.

This is the definition of default constructor. A constructor that supplies default arguments for all its parameters can be called without argument, thus fits the definition.

like image 96
Yu Hao Avatar answered Sep 28 '22 08:09

Yu Hao


By definition, a default constructor is one that can be called without arguments. Yours cleary fits that definition, since both parameters have default value.

The asnwer to "why" is, I'd say, simply because C++ standard says so. The choice of constructor to be called is done by overload resolution based on number and types of parameters, just like with other functions.

like image 37
jrok Avatar answered Sep 28 '22 08:09

jrok


The feature of constructor overload allow the compiler to infer which constructor to call based on the passed arguments. The default constructor is just the constructor which is resolved for no arguments, as in

A a;

or

A a=A();

And again due to parameters overloading only a single constructor can be resolved for each set. So, if all parameters have default values => it is ok to call 'A()' => it is the default constructor.

like image 36
Uri Cohen Avatar answered Sep 28 '22 08:09

Uri Cohen