yeates
yeates

Reputation: 41

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

I'm practicing the reflection in C++, and the code is as follows:

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

Upvotes: 0

Views: 247

Answers (2)

Drew Dormann
Drew Dormann

Reputation: 63957

It's a carryover from C.

In C

void* (*)(void) is a pointer to a function that takes no parameters.

void* (*)() is a pointer to a function that takes unspecified parameters.

In C++

Both mean no parameters.

() is idiomatic and (void) was allowed for backwards compatibility.

Upvotes: 4

Ilan Keshet
Ilan Keshet

Reputation: 604

It is just another way of representing no parameters.

typedef void* (*PTRCreateObject)(void);  

and

typedef void* (*PTRCreateObject)();

are both equivalent.

the return signature void* is just returning a pointer to some unknown type.

Upvotes: 1

Related Questions