I’m trying to do something with pointers to function members that I cant’ figure out how this works.
I have a class A that looks something like this
class A
{
public:
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
float Divide (float a, float b) { return a/b; }
};
I want to declare a pointer to A and then pass to another function a function pointer to one of the members of A.
Something likes this:
void Will_Get_Function_Pointer(float a, float b, float (A::*pt2Func)(float, float))
{
(...)
}
Where I would call it with something like this
A* myA = new A;
Will_Get_Function_Pointer(1.00,2.00, &myA->Minus)
I can’t use static/const members because In my final implementation A will point to a specific A in a collection of A objects in something that will look like this
vector<A> vecA;
myA = &vecA[xxx]
Doing this fails miserably
typedef float (A::*pA)(float x, float y);
pA p = &myA->Minus;
and the compiler tells me to use &A::Minus but that wont work for me.
Can I even do this?
Cheers
You cannot. You should use something like
And calls it as
Simple example.
http://liveworkspace.org/code/79939893695e40f1761d81ba834c5d15