Why default argument constructor is called as default constructor

后端 未结 4 684
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-13 02:13
Class A {
public:
       A(int i = 0, int k = 0) {} // default constructor WHY ??
       ~A() {}
};
int main()
{
  A a; // This creates object using defined default          


        
相关标签:
4条回答
  • 2021-01-13 02:19

    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.

    0 讨论(0)
  • 2021-01-13 02:25

    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.

    0 讨论(0)
  • 2021-01-13 02:30

    According to c++ standard a default constructor is the one which can be called without arguments.It is also the reason for your quesrion.

    0 讨论(0)
  • 2021-01-13 02:43

    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.

    0 讨论(0)
提交回复
热议问题