C++参数默认值示例

#include <iostream>
using namespace std;

void Alert(int a, int b=0, int c=0)
{
    cout<<"a:"<<a<<"\t"<<"b:"<<b<<"\t"<<"c:"<<c<<endl;
}

int main()
{
    Alert(1);
    Alert(1,2);
    Alert(1,2,3);
    Alert(1,0,3);  //Alert(1,,3)这是错的
    return 0;
}

说明

·参数默认值最好在函数声明时指定;
·函数有多个参数时,当为某个参数指定了默认值时,则其后面的参数都必须指定默认值;
·调用函数时,如果为某个有默认值的参数传了值,则其前面的参数都必须传值(有默认值的也得传);

评论: 0 | 引用: 0 | 查看次数: 4817
发表评论
登录后再发表评论!