J
jolie.demiranda
Dear all,
I'm fairly new to C++ so forgive me is this topic may have been
convered before... I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:
class A {
public:
double (A::*ptrToFunction)(double);
}
// and NOTHING else is in class A.
class B : public A {
public:
double function1(double);
double function2(double);
double function3(double);
}
// and NOTHING else is in class B.
Later on, I would like to do the following:
class ApplyCoolFunctions {
public:
double apply(A &a);
}
where
ApplyCoolFunctions::apply(A *a) {
a->*ptrToFunction(20.0);
}
The idea being that I can feed in objects of class B to
ApplyCoolFUnctions, i.e. with calls like:
B b;
b.ptrToFunction = &B::function1;
ApplyCoolFunctions(&b);
b.ptrToFunction = &B::funcion2;
ApplyCoolFunction(&b);
But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.
Any help with be greatly appreciated.
Thanks,
Jolie
I'm fairly new to C++ so forgive me is this topic may have been
convered before... I'm having a problem with pointers to member
functions. Essentially what I want to achieve is the following:
class A {
public:
double (A::*ptrToFunction)(double);
}
// and NOTHING else is in class A.
class B : public A {
public:
double function1(double);
double function2(double);
double function3(double);
}
// and NOTHING else is in class B.
Later on, I would like to do the following:
class ApplyCoolFunctions {
public:
double apply(A &a);
}
where
ApplyCoolFunctions::apply(A *a) {
a->*ptrToFunction(20.0);
}
The idea being that I can feed in objects of class B to
ApplyCoolFUnctions, i.e. with calls like:
B b;
b.ptrToFunction = &B::function1;
ApplyCoolFunctions(&b);
b.ptrToFunction = &B::funcion2;
ApplyCoolFunction(&b);
But... it doesn't work!! Is what I'm doing even possible? Or does the
fact that function1, function2, etc... are NOT defined in A mean that
what I am trying to do is impossible.
Any help with be greatly appreciated.
Thanks,
Jolie