函数参数列表几项说明(c++)

一、函数参数列表中如果前面的做了初始化,那么后面的都应该进行初始化

例如:

① int fun(int a,int b=10,int c=11); //正确

② int fun(int a,int b=10,int c);//不正确

二、声明和定义部分不能重复初始化,无论你给的值是否相同

例如:

①/*不正确*/

int fun(int a,int b=10,int c=11);

int fun(int a,int b=10,int c)

{

      return 0;

}

②/*正确*/

int fun(int a,int b=10,int c=11);

int fun(int a=1,int b,int c)

{

    return 0;

}

三、对于有参数并且没有初始化的一定要传参数,对于已经初始化了的可以不传参数

       例如:

       ①/*不正确*/

int fun(int a,int b=10,int c=11);

int fun(int a,int b,int c)

{

      return 0;

}

int main()

{

      int a(1),b(0);

      cout<<fun();

      return 0;

}

②/*正确*/

int fun(int a,int b=10,int c=11);

int fun(int a,int b,int c)

{

      return 0;

}

int main()

{

      int a(1),b(0);

      cout<<fun(a);

      return 0;

}

值得说明的是,在缺省参数的情况下,编译器做的只是一个简单的从左到右的匹配,并不会智能选择。

例如:

①/*不正确*/

int fun(int a,int b=10,int *c=NULL);

int fun(int a,int b,int *c)

{

      return a+b;

}

int main()

{

      int a(1),b(0);

      int c=10;

      cout<<fun(a,&c)<<endl;

      return 0;

}

②/*正确*/

int fun(int a,int b=10,int *c=NULL);

int fun(int a,int b,int *c)

{

            return a+b;

}

int main()

{

      int a(1),b(0);

      int c=10;

      cout<<fun(a)<<endl;

      cout<<c;

      return 0;

}

四、函数参数列表属于类域

说明程序:

class A

{

public :

    typedef int INT;

    #define INT16 int

private:

    INT test(INT a , INT b);

};

 

A::INT A::test(INT a, INT b)

{

    return 0;

}

原文地址:https://www.cnblogs.com/accipiter/p/2693487.html