我正在学习如何动态加载DLL,但我不明白的是这一行

typedef void (*FunctionFunc)();

我有几个问题。如果有人能回答这些问题,我会很感激。

为什么使用typedef ? 语法看起来很奇怪;void后面不应该有函数名之类的吗?它看起来像一个匿名函数。 函数指针是用来存储函数的内存地址的吗?

所以我现在很困惑;你能给我解释一下吗?


当前回答

如果你能使用c++ 11,你可能想要使用std::function和using关键字。

using FunctionFunc = std::function<void(int arg1, std::string arg2)>;

其他回答

typedef is used to alias types; in this case you're aliasing FunctionFunc to void(*)(). Indeed the syntax does look odd, have a look at this: typedef void (*FunctionFunc) ( ); // ^ ^ ^ // return type type name arguments No, this simply tells the compiler that the FunctionFunc type will be a function pointer, it doesn't define one, like this: FunctionFunc x; void doSomething() { printf("Hello there\n"); } x = &doSomething; x(); //prints "Hello there"

Typedef是一种将名称与类型关联起来的语言构造。 例如,您可以使用与原始类型相同的方式使用它

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)();

正如您所看到的,您可以用上面给出的定义来替换经过类型定义的名称。

难点在于C和c++中指向函数的指针语法和可读性,而typedef可以提高这类声明的可读性。但是,语法是合适的,因为函数与其他更简单的类型不同,它可能有一个返回值和参数,因此有时对函数指针的声明冗长而复杂。

对于指向函数数组的指针和其他一些更间接的类型,可读性可能会变得非常棘手。

来回答你的三个问题

Why is typedef used? To ease the reading of the code - especially for pointers to functions, or structure names. 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 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这个词,在c++中,声明将声明一个类型为指针的变量FunctionFunc,指向无参数的函数,返回void。

使用typedef,它将FunctionFunc定义为该类型的名称。

#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) );
}

如果你能使用c++ 11,你可能想要使用std::function和using关键字。

using FunctionFunc = std::function<void(int arg1, std::string arg2)>;