7

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(); } 
8
  • 2
    static functions obviously cannot access (non-static) member variables.. i'm not sure what was your first and last try.. could you show the related code? Commented Feb 24, 2012 at 11:19
  • Post the code containing the declaration and definition of your static function. Otherwise it is difficult to understand what is being asked. Static functions do not have this pointer so you can not do this-> . You need to pass the object it should work upon as an parameter to the function. Commented Feb 24, 2012 at 11:19
  • this is not valid in a static context because there's no class instance. Commented Feb 24, 2012 at 11:22
  • The code is quite complex which is why I didn't post it, I'll try to write something which states the problem in "code mode" (not sure how long it will take). Commented Feb 24, 2012 at 11:23
  • 2
    @SkippyleGrandGourou Since you can't change the function prototype you will have to use a global pointer or static member variable that you set to point to this before your function is called. This won't work well though if you have more than one instance of your class. See my answer. Commented Feb 24, 2012 at 11:55

4 Answers 4

3

Pointers to non-static member functions are a bit tricky to deal with. The simplest workaround would just be to add an opaque pointer argument to your function which you can then cast as a pointer to 'this', then do what you need with it.

Here's a very simple example:

void doSomething(int (*callback)(void *usrPtr), void *usrPtr) { // Do stuff... int value = callback(usrPtr); cout << value << "\n"; } class MyClass { public: void things() { value_ = 42; doSomething(myCallback, this); } private: int value_; static int myCallback(void *usrPtr) { MyClass *parent = static_cast<MyClass *>(usrPtr); return parent->value_; } }; int main() { MyClass object; object.things(); return 0; } 

In this example myCallback() can access the private value_ through the opaque pointer.

If you want a more C++-like approach you could look into using Boost.Function and Boost.Bind which allow you to pass non-static member functions as callbacks:

void doSomething(boost::function<int ()> callback) { // Do stuff... int value = callback(); cout << value << "\n"; } class MyClass { public: void things() { value_ = 42; doSomething(boost::bind(&MyClass::myCallback, this)); } private: int value_; int myCallback() { return value_; } }; int main() { MyClass object; object.things(); return 0; } 

If you really can't change the function prototype you could use a global pointer, but that opens up all sorts of issues if you will ever have more than one instance of your class. It's just generally bad practice.

class MyClass; static MyClass *myClass; void doSomething(int (*callback)()) { // Do stuff... int value = callback(); cout << value << "\n"; } class MyClass { public: void things() { value_ = 42; myClass = this; doSomething(myCallback); } private: int value_; static int myCallback() { return myClass->value_; } }; int main() { MyClass object; object.things(); return 0; } 
Sign up to request clarification or add additional context in comments.

Comments

2

Following spencercw's suggestion below the initial question I tried the "static member variable that you set to point to this" solution (the global variable would have been tricky and dangerous within the context of the software suite).

Actually I figured out there was already something like this implemented in the code (which I didn't write) :

static void* currentObject; 

So I just used it, as

((MyClass*)currentObject)->SomeComputation(); 

It does work, thanks !!!

Comments

0

non-reentrant and non-thread-safe way is to pass "this" address using global variable.

Comments

0

You can move the result = SomeComputation(); out of your static function and place it in BigFunction right before your call to the static function.

2 Comments

I think you still do. Because you are sending result to the static function by reference.
I guess one could indeed use the result argument as an input, I didn't think about it like that. But remember this is a simplified example. Actually it couldn't work in my case because one of the things I included in the SomeComputation function is to update many values of the object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.