【发布时间】:2013-11-09 17:21:39
【问题描述】:
我正在尝试理解参数默认设置,我让这段代码将 3 个参数传递给一个函数并返回产品。
我怎样才能使cout<<"3---... 和下面的“4---”行中的代码使用参数中的默认值?在底部查看我的输出
代码
#include "stdafx.h"
#include<iostream>
using namespace std;
int product(char str,int a=5, int b=2);
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"1---"<<product('A',40,50)<<endl;
cout<<"2---"<<product('A')<<endl;
cout<<"3---"<<product('A',NULL,50)<<endl;
cout<<"4---"<<product('A',40)<<endl;
int retValue=product('A',40,50);
cout<<"5---"<<retValue<<endl;
system("pause");
return 0;
}
int product(char str,int a, int b){
return(a*b);
}
输出
1---2000
2---10
3---0
4---80
5---2000
按任意键继续。 . .
【问题讨论】:
-
大喊真的有必要吗?标题本身就足够突出。
-
我不明白 4 有什么问题,但 3 是不可能的,除非您编写另一个函数或使用类似 Boost 的命名参数。
-
@chris 我是怎么做到的?喊?我只是像输入 CODE 一样输入了 OUTPUT。归咎于“黑盒”转型。还是谢谢
-
我指的不是这个。我指的是标题。
标签: c++ function parameter-passing