This question already has answers here:
Typedef function pointer?
                                
                                    (6个答案)
                                
                        
                                2年前关闭。
            
                    
浏览一些代码时,我在头文件中发现了以下内容:

typedef void (*print_type) (const char*);


这行是什么意思?我知道typedef用于制作特定的单词(在我们的情况下为*print_type)来表示特定的类型(在我们的情况下为const char*)。

但是我的问题是为什么我们必须使用void

最佳答案

这个:

void foo(const char*);


foo声明为接受const char*参数并返回void的函数(即,它不返回任何内容)。 (必须在某个地方定义它才能调用它。)

这个:

void (*bar)(const char*);


bar定义为指针对象。它是指向与上述foo具有相同类型的函数的指针。

通过添加typedef关键字,这可以:

typedef void (*print_type)(const char*);


定义的print_type,不是作为该函数指针类型的对象,而是作为该函数指针类型的别名。

关于c++ - typedef void(* print_type)(const char *); ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47780330/

10-11 21:58
查看更多