I have an array of pointer-to-member functions in class A and need to access the elements in class B. My problem is either always getting a 1 returned when trying to access an array element or types not matching with each other.
What I've got so far is:
A.h:
#include <vector>
class A {
public:
typedef void(*func_ptr)(void);
A();
void func1();
void func2();
void func3();
std::vector<void(A::*)()> aFuncs;
private:
void appendFunc(void(A::*function)());
};
A.cpp
#include "A.h"
void A::func1 {...}
void A::func2 {...}
void A::func3 {...}
void A::appendFunc(void(A::*function)()) {
aFuncs.push_back(function);
}
A::A() {
appendFunc(&A::func1);
appendFunc(&A::func2);
appendFunc(&A::func3);
}
B.h
#include "A.h"
class B {
A a;
void test(int value);
};
B.cpp
#include "B.h"
void B::test(int value) {
// here i need to access the elements of the array aFuncs, so that i can
// call the functions of A
// something like
a.aFuncs[value];
}
The problem here e.g. is, that a.aFuncs[value] always returns 1 if I use it like this.
The only thing that worked for me so far is:
void B::test(int value) {
typedef void (a::*fn)();
fn funcPtr = &a::func1;
(a.*funcPtr)();
}
But that solution is not using the array so not really helpful right now. Can someone help me with this problem? Is there something fundamental im not understanding?