Pushing Class Member Function Pointers Into A Vector
I cant seem to figure out how to accomplish this...
I have a class containing a vector of functions pointers, of which, I would like to point to specific class instances' member functions.
Such as: (See below)
Although, I cant seem to even get this to compile.
How do I properly push these member functions into the vector?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <vector>
#include <iostream>
using namespace std;
typedef int (*functionPtr) (int x);
class AwesomeClass
{
public:
vector<functionPtr> vector_of_functionPtrs;
AwesomeClass(){}
~AwesomeClass(){}
};
class SuperClass
{
public:
int i;
SuperClass(int x){ i = x; }
~SuperClass(){}
int function1(int a){return a+i; }
int function2(int b){return b+i; }
};
void main()
{
AwesomeClass aC = AwesomeClass();
SuperClass sC1 = SuperClass(2);
SuperClass sC2 = SuperClass(4);
aC.vector_of_functionPtrs.push_back(&sC1.function1);
aC.vector_of_functionPtrs.push_back(&sC1.function2);
aC.vector_of_functionPtrs.push_back(&sC2.function1);
aC.vector_of_functionPtrs.push_back(&sC2.function2);
int x = 0;
for (int i = 0; i < aC.vector_of_functionPtrs.size; i++){ x += aC.vector_of_functionPtrs.at(i)(5); cout << x << endl; }
system("PAUSE");
}
|
Topic archived. No new replies allowed.