I have a 3rd party library and I have to assign function pointers like this:
FunctionsStruct functionsStruct;
functionsStruct.firstFunctionPointer = myFirstFunction;
functionsStruct.secondFunctionPointer = mySecondFunction;
functionsStruct.thirdFunctionPointer = ...;
In myFirstFunction, mySecondFunction I use shared variables. It looks like this:
void myFirstFunction(int a, int b) {
sharedVariable = 56 * a;
...
}
void mySecondFunction(int c) {
sharedVariable = 21 + c;
...
}
Of course I can write a code in this way:
int sharedVariable;
void myFirstFunction(int a, int b) {
sharedVariable = 56 * a;
...
}
void mySecondFunction(int c) {
sharedVariable = 21 + c;
...
}
void setFunctionPointers() {
FunctionsStruct functionsStruct;
functionsStruct.firstFunctionPointer = myFirstFunction;
functionsStruct.secondFunctionPointer = mySecondFunction;
functionsStruct.thirdFunctionPointer = ...;
}
But I would like to avoid global / static variables + have classes.
So the best for me is something like this ( I know that I can't assign member function address to function pointers - I'm looking for something similar ) :
class A {
public:
A(int sharedVar): sharedVariable(sharedVar) {};
int sharedVariable {};
void myFirstFunction(int a, int b);
void mySecondFunction(int c);
}
class B {
private:
A a;
public:
B() {};
void setFunctionPointers();
}
void B::setFunctionPointers() {
functionsStruct.firstFunctionPointer = &a::myFirstFunction;
functionsStruct.secondFunctionPointer = &a::mySecondFunction;
functionsStruct.thirdFunctionPointer = ...;
}
So what is the nicest solution?