问题描述
我正在学习如何动态地加载DLL的,但我不明白的是这行
I'm learning how to dynamically load DLL's but what I don't understand is this line
typedef void (*FunctionFunc)();
我有几个问题。如果有人能够回答他们,我将不胜感激。
I have a few questions. If someone is able answer them I would be grateful.
- 为什么
的typedef
使用吗? - 的语法看起来很奇怪;
无效后
应该有不是一个函数名还是什么?它看起来像一个匿名函数。 - 是创建用于存储函数的存储器地址的函数指针?
- Why is
typedef
used? - The syntax looks odd; after
void
should there not be a function name or something? It looks like an anonymous function. - Is a function pointer created to store the memory address of a function?
所以我此刻的困惑;你能澄清的东西给我吗?
So I'm confused at the moment; can you clarify things for me?
推荐答案
的typedef
是一个名称,一个类型相关联的语言结构。
您可以使用它你可以使用原始类型相同的方式,比如
typedef
is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance
typedef int myinteger;
typedef char *mystring;
typedef void (*myfunc)();
使用它们像
myinteger i; // is equivalent to int i;
mystring s; // is the same as char *s;
myfunc f; // compile equally as void (*f)();
正如你所看到的,你可以只与它上面给出的定义替换的通过typedef 的名称。
的难点在于指针在C和C ++函数的语法和可读性,而的typedef
可改善这种声明的可读性。但是,语法是合适的,因为功能 - 不像其他简单的类型 - 可能有一个返回值和参数,从而函数指针的有时是漫长和复杂的声明
The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef
can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.
可读性可能开始与函数指针数组真正棘手,以及其他一些更为间接的味道。
The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.
要回答你的三个问题。
-
为什么要使用的typedef?
为缓解code的读数 - 特别是函数指针或结构的名称。
Why is typedef used?To ease the reading of the code - especially for pointers to functions, or structure names.
的语法看起来很奇怪(在指针函数声明)
这种语法不明显阅读,至少刚开始的时候。使用的typedef
声明,而不是减轻了阅读
The syntax looks odd (in the pointer to function declaration)That syntax is not obvious to read, at least when beginning. Using a typedef
declaration instead eases the reading
创建存储函数的内存地址的函数指针?
是,函数指针存储功能的地址。这有什么好做的的typedef
结构,只有缓解一个程序的写入/读取;编译器在编译的实际code之前刚刚扩展的typedef定义。
Is a function pointer created to store the memory address of a function?Yes, a function pointer stores the address of a function. This has nothing to do with the typedef
construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.
例如:
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
这篇关于typedef的函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!