I get a compiler error at the line func = &Fred::fa;
saying:
[Error] '((Fred*)this)->Fred::func' cannot be used as a member pointer
since it is of type 'fptr {aka double (*)(int, int)}
. However, I know that if I define the typedef as a class inside the class
typedef double (Fred::*fptr)(int x, int y);
then it will not error it. But I want to be sure that the typedef is defined outside the class.
What am I doing wrong?
#include <iostream>
typedef double (*fptr)(int x, int y);
class Fred
{
private:
//typedef double (Fred::*fptr)(int x, int y);
fptr func;
public:
Fred()
{
func = &Fred::fa;
}
void run()
{
int foo = 10, bar = 20;
std::cout << (this->*func)(foo,bar) << '\n';
}
double fa(int x, int y)
{
return (double)(x + y);
}
};
int main ()
{
Fred f;
f.run();
return 0;
}