注意:默认参数只能是参数列表的最后几项,并且是连续的
#include <iostream> using namespace std; //============================================================================ //总结:默认参数只能是后几位 //============================================================================ void add1(int a = 11, int b=22){ cout<<a+b<<endl; } int add11(int a = 11, int b=22){ return a+b; } void coutStr1(const char *s = "[字符串指针]"){ cout << "输出指针字符串:" <<s<< endl; } void coutStr2(const char s[]= "[字符串数组]"){ cout << "输出字符串数组:" <<s<< endl; } int main(){ cout << "函数默认参数:" << endl; //cout << "加法计算结果为:" <<(long)add1<< endl; cout << "加法计算结果为:" <<add11<< endl; // 是地址 cout << "加法计算结果为:" <<add11()<< endl; cout << "加法计算结果为:" <<(*add11)()<< endl; cout << "加法计算结果为:" <<add11(222)<< endl; //cout << "coutStr1:" <<coutStr1<< endl; coutStr1(); //cout << "coutStr2:" <<coutStr2<< endl; coutStr2(); cout << "end." << endl; return 0; }