c++可变参数函数

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_end

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

测试程序:

#include <algorithm>
#include <string.h>
#include <vector>
#include <queue>
#include <iostream>
#include "cstdarg"


using namespace std;


void getUrl(char* host, ...){
    va_list ptr;


    va_start(ptr, host);
    printf("Host: %s\n", host);


    int port = va_arg(ptr, int);
    printf("Port: %d\n", port);


    char* file = va_arg(ptr, char*);
    printf("File: %s\n", file);


    char ch;
    while (1){
        ch = va_arg(ptr, int);


        if (ch == 'e'){
            break;
        }
        printf("%c\n", ch);
    }
    printf("\n");
}


int main(){
    getUrl("baidu.com", 80, "home.html", 'a', 'b', 'e');
    getUrl("tom.com", 8080, "home.html", 'a', 'b', 'a', 'e');
    return 0;
}

这个例子实现的是用一个函数来获取url各部分的内容,一个奇怪的地方是当将代码ch = va_arg(ptr, int)换为ch = va_arg(ptr, char)时,程序会崩溃,当把char换为short时同样。

这里面涉及到内存对齐的内容,char类别的参数,压栈后实际上用了4个字节来存,当把4个字节的数据写到两个或者一个字节大小的类型数据中时,自然会出错。

留待后面继续分析!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值