Why default argument constructor is called as default constructor

后端 未结 4 682
爱一瞬间的悲伤
爱一瞬间的悲伤 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.

提交回复
热议问题