I have a class and one of its member functions is actually a function pointer. That way the user can overwrite what does this function do. I unfortunately have some difficulties running this function. I use g++ to compile.
Below is the code:
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2+phit;
}
class Object{
public:
Object() {
fcn_ptr = (double (*)(const double &)) &Object::fcn_default;
}
double (*fcn_ptr)(const double &) = NULL;
private:
double fcn_default(const double &phit){
return 3.2+phit;
}
};
int main(){
Object test1,test2;
cout << (test1.*fcn_ptr)(2) << endl; // can not compile here
test2.fcn_ptr = &fcn_mod;
cout << (test2.*fcn_ptr)(2) << endl; // and here
cout << "test" << endl;
}
The error message is:
erreur: ‘fcn_ptr’ was not declared in this scope
+++++ UPDATE 1 +++++
Taking into account the comments, here is a cleaner version:
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2+phit;
}
double fcn_default(const double &phit){
return 3.2+phit;
}
class Object{
public:
double (*fcn_ptr)(const double &) = NULL;
Object() {
fcn_ptr = &fcn_default;
}
};
int main(){
Object test1,test2;
cout << (test1.*fcn_ptr)(2) << endl; // can not compile here
test2.fcn_ptr = &fcn_mod;
//cout << (test2.*fcn_ptr)(2) << endl; // and here
cout << "test" << endl;
}
This one is also a better approach:
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2+phit;
}
class Object{
public:
Object() {
fcn_ptr = &fcn_default;
}
double (*fcn_ptr)(const double &) = NULL;
private :
static double fcn_default(const double &phit){
return 3.2+phit;
}
};
int main(){
Object test1,test2;
cout << (test1.*fcn_ptr)(2) << endl; // can not compile here
test2.fcn_ptr = &fcn_mod;
cout << (test2.*fcn_ptr)(2) << endl; // and here
cout << "test" << endl;
}
+++++ UPDATE 2 +++++
What if I want to access members of the class through this function?
The simplest solution will not be able to give access to the class member (aa) from within fcn_mod.
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2 + phit + aa*0.001; // can not compile here
}
class Object{
public:
Object() {
fcn_ptr = &Object::fcn_default;
aa = 4;
}
double (*fcn_ptr)(const double &) = NULL;
double aa;
private:
static double fcn_default(const double &phit){
return 3.2 + phit + aa*0.001; // and here
}
};
int main(){
Object test1,test2;
cout << (test1.fcn_ptr)(2) << endl;
test2.fcn_ptr = &fcn_mod;
cout << (test2.fcn_ptr)(2) << endl;
cout << "test" << endl;
}
But the solution using std::bind and std::function does not either. Can I pass a kind of "static" parameter to the function pointer?
I added a gateway function.
#include <iostream>
using namespace std;
double fcn_mod(const double &phit){
return 1.2 + phit;
}
class Object{
public:
Object() {
fcn_ptr = &Object::fcn_default;
aa = 4;
}
double do_something(const double &phit){
return this->fcn_ptr(phit+aa*0.001);
}
double (*fcn_ptr)(const double &);
double aa;
private:
static double fcn_default(const double &phit){
return 3.2 + phit;
}
};
int main(){
Object test1,test2;
cout << test1.do_something(2) << endl; // can not compile here
test2.fcn_ptr = &fcn_mod;
cout << test2.do_something(2) << endl; // and here
cout << "test" << endl;
}