C&C++函数指针

来源:互联网 发布:mac m4a格式转换mp3 编辑:程序博客网 时间:2024/06/16 00:21

今天在阅读libcurl的源码的时候,发现里边定义函数指针的方法,与平时自己所用方式有所不同。详细分析了一下。

libcurl的代码中,定义了一组发送数据的函数指针。如下所示:

//代码目录: lib/urldata.hstruct connectdata {......Curl_send *send[2];......};
其中,Curl_send定义如下:
//代码目录: lib/urldata.h/* return the count of bytes sent, or -1 on error */typedef ssize_t (Curl_send)(struct connectdata *conn, /* connection data */                            int sockindex,            /* socketindex */                            const void *buf,          /* data to write */                            size_t len,               /* max amount to write */                            CURLcode *err);           /* error to return */

感到疑惑的是,平时我们要使用typedef定义函数指针的话,一般都是写成下面形式:
//定义一个函数指针,所指向函数接收一个整形参数,并返回整形值。typedef int (*pFunc)(int);
但是,curl_send的定义中,并没有带上指针符号。在查阅一些资料后,发现这样的定义方法也是可以的。

于是,写了下面的程序验证了一下。

#ifdef __cplusplus#include <iostream>#else#include <stdio.h>#endifint testFunc(int para){#ifdef __cplusplusstd::cout << "C++ parameter is: " << para << std::endl;#elseprintf("C parameter is: %d\n", para);#endifreturn 0;}int main(){typedef int (pTestFunc)(int);pTestFunc *pFunc = testFunc;   //方式1:ok·pFunc(1111);                   //方式2:ok(*pFunc)(2222);                //方式3:okpTestFunc *pFunc2 = &testFunc; //方式4:okpFunc2(3333);return 0;}
如果将上面程序保存为C程序文件(.c),进行编译,得到下面运行结果:
C parameter is: 1111
C parameter is: 2222
C parameter is: 3333

如果保存为C++程序文件(.cpp),得到运行结果:
C++ parameter is: 1111
C++ parameter is: 2222
C++ parameter is: 3333

从上面结果可以看到:
1.采用与curl_send相同的函数声明方式,也是可以的。如上面的pTestFunc的定义。
2.对于函数指针pFunc的初始化,不管采用哪种方式都是可以的。参考上面的方式1与方式4。
3.而对于函数指针pFunc的调用,也可以采用不同的方式,参考方式2与方式3。
4.不管对于C还是C++,这些定义和使用函数指针的方式都是支持的。

当然,我们也可以采用传统的函数指针声明方式,如下程序所示:

int main(){typedef int (*pTestFunc)(int);pTestFunc pFunc = testFunc;   //方式5:okpFunc(1111);                  //方式6:ok(*pFunc)(2222);               //方式7:okpTestFunc pFunc2 = &testFunc; //方式8:okpFunc2(3333);return 0;}
运行结果与前面所述完全相同。

0 0