Syntax of How to Call a Member Function via a Function Pointer
-
Hi, I have a Base Class Header File as follows,
// Header File:
class MyClass,// Advance Declaration
typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);class MyClass{
// Lots of things
PFN_DO_SOMETHING m_pfnDoSomething;
void Act(void* pData);
}//Implementation File:
void MyClass::Act(void* pData){
m_pfnDoSomething(pData);
}Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)
Bram van Kampen
-
Hi, I have a Base Class Header File as follows,
// Header File:
class MyClass,// Advance Declaration
typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);class MyClass{
// Lots of things
PFN_DO_SOMETHING m_pfnDoSomething;
void Act(void* pData);
}//Implementation File:
void MyClass::Act(void* pData){
m_pfnDoSomething(pData);
}Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)
Bram van Kampen
The solution:
class MyClass{
// Lots of things typedef void (\*MyClass::PFN\_DO\_SOMETHING)(void\* pData); PFN\_DO\_SOMETHING m\_pfnDoSomething; void Act(void\* pData);
};
//Implementation File:
void MyClass::Act(void* pData){
m_pfnDoSomething(pData);
}Your errors:
- The asterisk in the
typedef
is at the wrong place. - Place the
typedef
inside the class to avoid the forward declaration which would not work here with the correcttypedef
. - The function member name is
m_pfnDoSomething
(notpfnDoSomething
).
Note also that there is no need to use the class prefix with the
typedef
. You may also use:typedef void (*PFN_DO_SOMETHING)(void* pData);
- The asterisk in the
-
Hi, I have a Base Class Header File as follows,
// Header File:
class MyClass,// Advance Declaration
typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);class MyClass{
// Lots of things
PFN_DO_SOMETHING m_pfnDoSomething;
void Act(void* pData);
}//Implementation File:
void MyClass::Act(void* pData){
m_pfnDoSomething(pData);
}Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)
Bram van Kampen
-
Hi, I have a Base Class Header File as follows,
// Header File:
class MyClass,// Advance Declaration
typedef void (MyClass::*PFN_DO_SOMETHING)(void* pData);class MyClass{
// Lots of things
PFN_DO_SOMETHING m_pfnDoSomething;
void Act(void* pData);
}//Implementation File:
void MyClass::Act(void* pData){
m_pfnDoSomething(pData);
}Needless to say, 'pfnDoSomething' has been initialised elsewere. I boiled the question down to the essentials, the correct CPP syntax for calling a member function from a pointer to it. I get a Compiler error stating that 'pfnDoSomething' does not resolve to a function call. What is the correct syntax for calling this function. regards :)
Bram van Kampen
As per Jochen's answer but I would also add a guard against your forgetting to set the pointer checking it's not NULL. The typecast and advance definition is also unnecessary unless you actually need the type for something, the pointer will be checked for match when you attempt to set it with or without the typecast.
void MyClass::Act(void* pData){
if (m_pfnDoSomething) m_pfnDoSomething(pData);
}Here is a minimalist function ptr .. no need for forward delaration, typedef or even a name of field. All you need is the * dereference before the function ptr name it knows what you are doing from that and really all it needs is what the function looks like so when you try to set it, that it checks for match.
class MyClass{
void(*MyClass::m_pfnDoSomething)(void*);void MyClass::Act(void\* pData){ if (m\_pfnDoSomething) m\_pfnDoSomething(pData); }
};
Finally especially since your in a class I would suggest you don't pass in the void* data pointer but look at other options to typecast or marshal the data in a way that is a little safer.
In vino veritas