I was trying to assign a static member function to a functor as following:
class myClass
{
public:
static void (myClass::*fptr)();
static void myFun()
{
}
};
(void (myClass::*)()) myClass::fptr = &myClass::myFun; // doesn't work
int main()
{
myClass obj;
myClass::fptr = &myClass::myFun; // neither does this works
return 0;
}
The above assignment is not working.
At lest to check the type of the static member function(as I was not sure) I wrote the following statement in main
myClass::fptr = &myClass::myFun;
And the VS2010 intellisense displayed:
you can't assign a void ( * )() type to an entity of type void (myClass::*)()
I thought that type of static function should have been the latter(void ( * )())
I am also unable to figure out how to give definition for a static functor as it should be done for static data members.
I made the functor static because I want it to be independent of any instances of myClass.
Can somebody please elaborate on this topic of static functors and also on the type of the static member functions(as to why it is void(*)() in this case). Is it even possible to have a function pointer to a static member function?
EDIT:
I was able to work around this by using a typedef:
typedef void(*ptr)();
class myClass
{
....
static ptr fptr;
};
ptr myClass::fptr = & myClass::myFunc;
But still why is the type static member function void( * )() ?