关于c++的默认参数

首先建立了两个cpp文件
fun.cpp
int func02(int x,int y,int z)
{
return x+y+z;
}
main.cpp
#include

using namespace std;

extern int func02(int x,int y=25,int z=35);

void test04()
{
cout<<func02(100,200)<<endl;
cout<<func02(100)<<endl;
}
int main(int argc, char *argv[])
{
test04();
return 0;
}
运行结果为335
160
我在想,在声明和定义func02之时都没有对x设置默认值,如果我在输出之时不给x传参会怎样。
我尝试将第二个输出: cout<<func02(100)<<endl;修改为
cout<<func02()<<endl;
结果报错该语句参数过少,说明c++严格遵循语法,x没有参数直接报错。
我又在这个基础上,将声明处修改为extern int func02(int x=0,int y=25,int z=35);
运行后没有报错,输出的结果为335
60

猜你喜欢

转载自blog.csdn.net/Flywithdawn/article/details/104933446