I have to make some kind of bridge between two pieces of software, but am facing an issue I don't know how to deal with. Hopefully someone will have interesting and (preferably) working suggestions.
Here is the background : I have a C++ software suite. I have to replace some function within a given class with another function, which is ok. The problem is that the new function calls another function which has to be static, but has to deal with members of the class. This is this second function which is making me mad.
If the function is not static I get the following error :
error: argument of type ‘void (MyClass::)(…)’ does not match ‘void (*)(…)’ If I set it to static I get either the following error :
error: cannot call member function ‘void MyClass::MyFunction(const double *)’ without object or
error: ‘this’ is unavailable for static member functions depending on if I use or not the "this" keyword ("Function()" or "this->Function()").
And finally, the class object requires some arguments which I cannot pass to the static function (I cannot modify the static function prototype), which prevents me to create a new instance within the static function itself.
How would you deal with such a case with minimal rewriting ?
Edit : Ok, here is a simplified sample on what I have to do, hoping it is clear and correct :
// This function is called by another class on an instance of MyClass MyClass::BigFunction() { … // Call of a function from an external piece of code, // which prototype I cannot change XFunction(fcn, some more args); … } // This function has to be static and I cannot change its prototype, // for it to be passed to XFunction. XFunction makes iterations on it // changing parameters (likelihood maximization) which do not appear // on this sample void MyClass::fcn(some args, typeN& result) { // doesn't work because fcn is static result = SomeComputation(); // doesn't work, for the same reason result = this->SomeComputation(); // doesn't work either, because MyClass has many parameters // which have to be set MyClass *tmp = new MyClass(); result = tmp->SomeComputation(); }
thispointer so you can not dothis->. You need to pass the object it should work upon as an parameter to the function.thisbefore your function is called. This won't work well though if you have more than one instance of your class. See my answer.