今天在阅读libcurl的源码的时候,发现里边定义函数指针的方法,与平时自己所用方式有所不同。详细分析了一下。
libcurl的代码中,定义了一组发送数据的函数指针。如下所示:
//代码目录: lib/urldata.h
struct 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 int (*pFunc)(int);但是,curl_send的定义中,并没有带上指针符号。在查阅一些资料后,发现这样的定义方法也是可以的。
于是,写了下面的程序验证了一下。
#ifdef __cplusplus
#include <iostream>
#else
#include <stdio.h>
#endif
int testFunc(int para)
{
#ifdef __cplusplus
std::cout << "C++ parameter is: " << para << std::endl;
#else
printf("C parameter is: %d\n", para);
#endif
return 0;
}
int main()
{
typedef int (pTestFunc)(int);
pTestFunc *pFunc = testFunc; //方式1:ok·
pFunc(1111); //方式2:ok
(*pFunc)(2222); //方式3:ok
pTestFunc *pFunc2 = &testFunc; //方式4:ok
pFunc2(3333);
return 0;
}如果将上面程序保存为C程序文件(.c),进行编译,得到下面运行结果:当然,我们也采用传统的函数指针声明方式,如下程序所示:
int main()
{
typedef int (*pTestFunc)(int);
pTestFunc pFunc = testFunc; //方式5:ok
pFunc(1111); //方式6:ok
(*pFunc)(2222); //方式7:ok
pTestFunc pFunc2 = &testFunc; //方式8:ok
pFunc2(3333);
return 0;
}运行结果与前面所述完全相同。
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/shltsh/article/details/46851061