代码行 1 中的"(void)"有什么用?

what's the use of '(void)' in code line 1?

本文关键字:什么 中的 代码 void      更新时间:2023-10-16

我正在练习C++中的反射,代码如下:

typedef void* (*PTRCreateObject)(void); 
class ClassFactory{
private:
map<string, PTRCreateObject>m_classMap;
ClassFactory(){};    
public:
void* getClassByName(string className);
void registClass(string name, PTRCreateObject method);
static ClassFactory& getInstance();    
};

这是 C 的遗留物。

在 C 语言中

void* (*)(void)是指向带参数的函数的指针。

void* (*)()是指向采用未指定参数的函数的指针。

在C++

两者都意味着没有参数。

()是惯用语,(void)允许向后兼容。

这只是表示无参数的另一种方式。

typedef void* (*PTRCreateObject)(void);  

typedef void* (*PTRCreateObject)();

两者都是等价的。

返回签名void*只是返回指向某个未知类型的指针。