I'm having trouble accomplishing this without using a wrapper class. This all worked in Visual C++ except for the function call itself, which gave the following error
"illegal on operands of type 'int (__thiscall FPclass::*)(void)' "
It comes down to two questions.
1. Is their a ANSI compliant way to create a vector of member function pointers in the class which owns these member functions.
2. Once this vector is filled, How do we call the member function through the vector.
NOTE: Bjarne Stroustrup discusses something similar in "the C++ programming language" in section 18.4.4.2
//______here's the code and thanks_____
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
class FPclass
{
public:
FPclass();
~FPclass();
int CallMemberFunction(int option);
private:
int func1(void);
vector<int ( FPclass::*)(void)> functions;
};
FPclass::FPclass()
{
functions.push_back(&FPclass::func1);
}
FPclass::~FPclass()
{
}
int FPclass::func1(void)
{
cout << "member function 1 has been called" << endl;
return 1;
}
int FPclass::CallMemberFunction(int option)
{
(*functions[option]) ();
return 1;
}
//***********************************************
int main(int argc, char* argv[])
{
FPclass fp;
fp.CallMemberFunction(0);
return 0;
}
"illegal on operands of type 'int (__thiscall FPclass::*)(void)' "
It comes down to two questions.
1. Is their a ANSI compliant way to create a vector of member function pointers in the class which owns these member functions.
2. Once this vector is filled, How do we call the member function through the vector.
NOTE: Bjarne Stroustrup discusses something similar in "the C++ programming language" in section 18.4.4.2
//______here's the code and thanks_____
#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
class FPclass
{
public:
FPclass();
~FPclass();
int CallMemberFunction(int option);
private:
int func1(void);
vector<int ( FPclass::*)(void)> functions;
};
FPclass::FPclass()
{
functions.push_back(&FPclass::func1);
}
FPclass::~FPclass()
{
}
int FPclass::func1(void)
{
cout << "member function 1 has been called" << endl;
return 1;
}
int FPclass::CallMemberFunction(int option)
{
(*functions[option]) ();
return 1;
}
//***********************************************
int main(int argc, char* argv[])
{
FPclass fp;
fp.CallMemberFunction(0);
return 0;
}