有谁知道该怎么做?!参数问题

zf0579 2003-01-18 04:55:21
以下程序非常简单,maxmin函数返回输入参数中较大的数值
问题是在main函数中step1、2、3(见main内注释)都能正常通过,我想让step4正常通过,可我不想填第一个参数,只想修改第二个参数,怎么做?step4在VC++6中不能编译通过。请教啦


// maxmin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int maxmin(int i = 1, int j = 1)
{
if (i > j)
return i;
else
return j;
}

int main(int argc, char* argv[])
{
int i;

//step1
i = maxmin();
printf("Number is %d\r\n", i);

//step2
i = maxmin(2);
printf("Number is %d\r\n", i);

//step3
i = maxmin(2,3);
printf("Number is %d\r\n", i);

//step4
i = maxmin(,4);
printf("Number is %d\r\n", i);

return 0;
}

...全文
57 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zf0579 2003-01-23
  • 打赏
  • 举报
回复
也可以考虑使用结构体的方式 也合适些吧
xdspower 2003-01-23
  • 打赏
  • 举报
回复
变通的方法
1。就是把参数变成一个,用结构体的域修改来完成参数改变
2。参数以一个字符串的形式传入,在函数中进行解析,这样就可以自定义规则了,并且可以完成多参数化。
zf0579 2003-01-21
  • 打赏
  • 举报
回复
是否考虑用类似main函数的方法,填默认值的方法 其实也不是很好
dyp_pro 2003-01-18
  • 打赏
  • 举报
回复
那你觉得哪样做比较理想?
不要掉大家胃口了,说说嘛:)
SwordMan2001 2003-01-18
  • 打赏
  • 举报
回复
上面的说法要实现起来有一定的麻烦,从通用性来考虑,USE_DEFAULT 必须是一个很特殊的数,且最好不能和可能用到的重复,因而使用范围是有限制的。
SwordMan2001 2003-01-18
  • 打赏
  • 举报
回复
我觉得这个是办不到的,不过定义一个常量比如 USE_DEFAULT 可能要好一些理解,其实SDK中很多函数的参数就是这样用的。当然,根据C++ 的语法,后面的参数是可以直接省略的。
i = tt.maxmin(USE_DEFAULT, 4);

C中好像也曾经允许过类似VB的使用逗号的用法,不过被认为安全性不好,后来就不用了。
zf0579 2003-01-18
  • 打赏
  • 举报
回复
我觉得这么做不是很理想 谢谢你 :)
popkiler 2003-01-18
  • 打赏
  • 举报
回复
class get_max
{
get_max();
int maxint(int x,int y);
int maxint(int x);

}
get_max::maxint(int x=1,int y=1)
{
return (x>y?x:y)
}

get_max::maxint(int x)
{
return (x>1?x:1)
}

void main()
{
get_max tt;

int i;

//step1
i = tt.maxmin();
printf("Number is %d\r\n", i);

//step2
i = tt.maxmin(2);
printf("Number is %d\r\n", i);

//step3
i = tt.maxmin(2,3);
printf("Number is %d\r\n", i);

//step4
i = tt.maxmin(,4);
printf("Number is %d\r\n", i);
}


老兄可以试着调试一下,大概原理就是运用类函数的重载来做的


zf0579 2003-01-18
  • 打赏
  • 举报
回复
实际应用是这样的
有很多参数,每次可能有些参数要用,有些参数不使用
所以要求参数有些可以填有些可以不填,现在就是要求达到这个目的
想知道怎么做,不要告诉我做些专用函数,我不想这么做
这里不是讨论C++语法,不要打感叹号,别以为我不懂语法

jadetiger 2003-01-18
  • 打赏
  • 举报
回复
缺省参数只能是最后连续几个,C++语法!

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧