首页IT科技c++函数参数缺省(C++函数参数中的省略号 waterday的博客 51CTO技术博客)

c++函数参数缺省(C++函数参数中的省略号 waterday的博客 51CTO技术博客)

时间2025-07-05 07:18:33分类IT科技浏览4561
导读:标签:C++ 函数 省略号 参数 休闲...

标签:C++ 函数 省略号 参数 休闲

原创作品             ,允许转载                     ,转载时请务必以超链接形式标明文章 原始出处               、作者信息和本声明             。否则将追究法律责任                     。http://waterday.blog.51cto.com/1032892/329079
C++允许定义形参个数和类型不确定的函数       。例如       ,C语言中的标准函数printf便使用这种机制      。在声明不确定形参的函数时      ,形参部分可以使用省略号“…             ”代替                     。“…                     ”告诉编译器                     ,在函数调用时不检查形参类型是否与实参类型相同              ,也不检查参数个数              。

例如:

void ConnectData(int i,...)

在上面的代码中      ,编译器只检查第一个参数是否为整型                    ,而不对其他参数进行检查      。

对于可变参数的函数              ,需要进行特殊的处理                    。首先需要引用 <stdarg.h> 头文件,然后利用va_list类型和va_start                    、va_arg       、va_end 3个宏读取传递到函数中的参数值              。

这几个宏的定义如下(在 ANSI C 中):

type va_arg( va_list arg_ptr, type );

void va_end( va_list arg_ptr );

void va_start( va_list arg_ptr, prev_param );

说明如下:

va_start

sets arg_ptr to the first optional argument in the list of arguments passed to the function. The argument arg_ptr must have va_list type. The argument prev_param is the name of the required parameter immediately preceding the first optional argument in the argument list. If prev_param is declared with the register storage class, the macro’s behavior is undefined. va_start must be used before va_arg is used for the first time.

【 va_start函数将参数arg_ptr设置为可变参数列表的第一个参数。参数arg_ptr的类型必须为va_list                    。参数prev_param是在可变参数列表之前的那一个参数                     。(也就是说在 ANSI C 中                    ,如果一个函数有可变参数                     ,那么在该可变参数前必须有一个明确定义的参数,否则无法调用函数 va_start              ,例如函数 int add(int i,...)是合法的                     ,而函数 int add(...)是不合法的。)】

va_arg

retrieves a value of type from the location given by arg_ptr and increments arg_ptr to point to the next argument in the list, using the size of type to determine where the next argument starts. va_arg can be used any number of times within the function to retrieve arguments from the list.

【 va_arg函数将返回 arg_ptr 所指位置的值       ,并将 arg_ptr 指向下一个参数 】

va_endAfter all arguments have been retrieved, va_end resets the pointer to NULL.

示例代码:

#include

#include

using namespace std;

int add(int pre,...) //求和函数

{

va_list arg_ptr;

int sum=0;

int nArgValue;

sum+=pre;

va_start(arg_ptr,pre);

do

{

nArgValue=va_arg(arg_ptr,int);

sum+=nArgValue;

}while(nArgValue!=0); //自定义结束条件是输入参数为0

va_end(arg_ptr);

return sum;

}

int main()

{

cout<

return 0;

}

参考:MSDN

http://winganson.blog.163.com/blog/static/12628477201021443515742/

http://hi.baidu.com/kiropower/blog/item/633d34d36b2b3fd3a9ec9a9c.html

本文出自 “waterday的博客       ” 博客             ,请务必保留此出处http://waterday.blog.51cto.com/1032892/329079

声明:本站所有文章                     ,如无特殊说明或标注       ,均为本站原创发布             。任何个人或组织      ,在未征得本站同意时                     ,禁止复制              、盗用                    、采集       、发布本站内容到任何网站       、书籍等各类媒体平台                     。如若本站内容侵犯了原著者的合法权益              ,可联系我们进行处理       。

创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!

展开全文READ MORE
uniapp getcontent图片(uni.getLocation和wx.getLocation方法调用无效,也不返回失败,解决方案!!!) 超参数的选择(Lesson 10.1 超参数优化与枚举网格的理论极限和随机网格搜索 RandomSearchCV)