Why default argument constructor is called as default constructor

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

提交回复
热议问题