Jac*_*vin 428 c c++ pointers typedef
我正在学习如何动态加载DLL,但我不明白的是这一行
typedef void (*FunctionFunc)();
Run Code Online (Sandbox Code Playgroud)
我有几个问题.如果有人能够回答他们,我将不胜感激.
typedef
用?void
应该有不是一个函数名还是什么?它看起来像一个匿名函数.所以我此刻很困惑; 你能为我澄清一下吗?
Rin*_*g Ø 438
typedef
是一种将名称与类型相关联的语言结构.
例如,您可以像使用原始类型一样使用它
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
Run Code Online (Sandbox Code Playgroud)
使用它们就像
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
Run Code Online (Sandbox Code Playgroud)
如您所见,您可以使用上面给出的定义替换typedefed名称.
困难在于指向C和C++中的函数语法和可读性,并且typedef
可以提高此类声明的可读性.但是,语法是合适的,因为函数 - 与其他更简单的类型不同 - 可能具有返回值和参数,因此有时指向函数的冗长和复杂的声明.
对于函数数组的指针,以及其他一些更间接的风格,可读性可能开始变得非常棘手.
回答你的三个问题
为什么使用typedef? 简化代码的读取 - 尤其是指向函数或结构名称的指针.
语法看起来很奇怪(在函数声明的指针中)
这种语法在读取时并不明显,至少在开始时是这样.使用typedef
声明可以简化阅读
是否创建了一个函数指针来存储函数的内存地址?
是的,函数指针存储函数的地址.这与typedef
仅简化程序的写入/读取的构造无关; 编译器只是在编译实际代码之前扩展typedef定义.
例:
typedef int (*t_somefunc)(int,int);
int product(int u, int v) {
return u*v;
}
t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456
Run Code Online (Sandbox Code Playgroud)
Jac*_*kin 179
typedef
用于别名类型; 在这种情况下,你混叠FunctionFunc
到void(*)()
.
确实语法看起来很奇怪,看看这个:
typedef void (*FunctionFunc) ( );
// ^ ^ ^
// return type type name arguments
Run Code Online (Sandbox Code Playgroud)不,这只是告诉编译器该FunctionFunc
类型将是一个函数指针,它没有定义一个,如下所示:
FunctionFunc x;
void doSomething() { printf("Hello there\n"); }
x = &doSomething;
x(); //prints "Hello there"
Run Code Online (Sandbox Code Playgroud)Che*_*Alf 31
如果没有这个typedef
词,在C++中声明会声明一个FunctionFunc
类型指针的变量,指向无参数的函数,返回void
.
用typedef
它来定义FunctionFunc
为该类型的名称.
如果您可以使用C++ 11,则可能需要使用std::function
和using
关键字.
using FunctionFunc = std::function<void(int arg1, std::string arg2)>;
Run Code Online (Sandbox Code Playgroud)
#include <stdio.h>
#include <math.h>
/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/
// typedef a primitive data type
typedef double distance;
// typedef struct
typedef struct{
int x;
int y;
} point;
//typedef an array
typedef point points[100];
points ps = {0}; // ps is an array of 100 point
// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)
// prototype a function
distance findDistance(point, point);
int main(int argc, char const *argv[])
{
// delcare a function pointer
distanceFun_p func_p;
// initialize the function pointer with a function address
func_p = findDistance;
// initialize two point variables
point p1 = {0,0} , p2 = {1,1};
// call the function through the pointer
distance d = func_p(p1,p2);
printf("the distance is %f\n", d );
return 0;
}
distance findDistance(point p1, point p2)
{
distance xdiff = p1.x - p2.x;
distance ydiff = p1.y - p2.y;
return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
380374 次 |
最近记录: |