I am trying to learn the concept of pointer to function . I have written a code Which is throwing errors Which i could not decipher . Please have a look
# include<iostream>
# include<stdio.h>
# include<conio.h>
using namespace std;
typedef int(*pt2Func)(int,int);
class A
{
private : int x;
int y;
public:
A(){}
A(int a, int b)
{
x=a;
y=b;
}
int sum(int a, int b){return a+b;}
int sub( int a , int b){return a-b;}
int mult( int a, int b){return a*b;}
pt2Func GetPtr2(const char c)
{
if (c == '+')
return ∑ // line 25
else if(c== '-')
return ⊂ // line 27
else if(c=='*')
return &mult; //line 29
}
void pass_ptr(int (*pointer_fn)(int,int))
{
int result;
result=(*pointer_fn)(10,5);
cout << " result is : " << result;
}
~A(){}
};
int main()
{
A a(0,5);
pt2Func=(a.GetPtr2)('+'); //line 43
int result = (a.*pt2Func)(5,10); //line 44
cout << "result is " << result;
getch();
return 0;
}
On compiling this program, I get the following errors in line 25,27,29:
cannot convert `int (A::*)(int, int)' to `int (*)(int, int)' in return
I also get error in line 43 and 44 Which are
expected primary-expression before='token'
typedefand then just use it.ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say&A::sum'`