The following code runs fine
#include <iostream>
using namespace std;
void fun()
{
cout<<"having some fun";
}
typedef void (*funptr)();
int main() {
// your code goes here
funptr p=fun;
p();
return 0;
}
This one doesn't work.
#include <iostream>
using namespace std;
class myclass
{
public:
void fun()
{
cout<<endl<<"having fun inside myclass"<<endl;
}
};
typedef void (myclass::*funptr)();
int main() {
// your code goes here
funptr p=myclass::fun; //THIS DOESN'T WORK. WHY?
myclass m;
(m.*p)();
return 0;
}
Why is & operator needed for member functions?