免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1796 | 回复: 7
打印 上一主题 下一主题

[C++] 帮忙下:) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-09-03 10:09 |只看该作者 |倒序浏览
我想写一个函数,参数未知,类型相同都为type*.
通过va_list来实现出了点问题.
void report(type* a,...)
{
   type *p=a;//指向第一个参数
   va_list args;
   va_start(args,a);
   while(p!=NULL)  //这里该用什么结束???????????????????
   {
       ...;
       p=va_arg(args,type*);//返回args列表的当前值,并指向列表的下一个位置
   }
   va_end(args);
}

论坛徽章:
0
2 [报告]
发表于 2008-09-03 10:16 |只看该作者
那你把参数都写在一个string里。。。自己分析不就好了?

论坛徽章:
0
3 [报告]
发表于 2008-09-03 10:21 |只看该作者
恩,是可以的
但我还是想弄明白,这个是怎么结束的

论坛徽章:
0
4 [报告]
发表于 2008-09-03 10:26 |只看该作者

回复 #1 wangsheng0415 的帖子

Notice also that va_arg does not determine either whether the retrieved argument is the last argument passed to the function (or even if it is an element past the end of that list). The function should be designed in such a way that the amount of parameters can be inferred in some way by the values of either the named parameters or the additional arguments already read.

论坛徽章:
0
5 [报告]
发表于 2008-09-03 10:29 |只看该作者
谢谢~
哦,明白了
那只能调用的时候将最后一个参数设成NULL了

论坛徽章:
0
6 [报告]
发表于 2008-09-03 10:30 |只看该作者
两种方法
1.设一个结束位,比如-1,当 p==-1,就不再读了
2.第一位表示参数的个数

论坛徽章:
0
7 [报告]
发表于 2008-09-03 10:31 |只看该作者
恩,都是好办法

论坛徽章:
0
8 [报告]
发表于 2008-09-03 10:31 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP