0

I have some trouble with assigning a callback function-pointer to member function. I have a callback function-pointer like this:

void (*callback)(float arg);

And my class looks like this:

class EXAMPLE_CLASS
{

public:
    /*
     * Standard Constructor
     */
    EXAMPLE_CLASS();

    /*
     * Standard Destructor
     */
    virtual ~EXAMPLE_CLASS();


    void test_callback(float arg)
    {
        y = arg;
    }

private:
    float y;

};

I want now to assign this callback function-pointer to the member callback function, how can I achieve this?

Edit I want to achieve something like this now:

EXAMPLE_CLASS obj;

callback = obj.test_callback;
HansPeterLoft
  • 489
  • 1
  • 9
  • 28
  • Make it `static void test_callback(float arg);`, then you can write `callback = EXAMPLE_CLASS::test_callback;` – Igor Tandetnik Apr 19 '20 at 12:35
  • Are you from C#? In C++, `obj.test_callback` is not allowed. You can create a function object taking `(EXAMPLE_CLASS&, float)` from `std::mem_fun`, and then `std::bind` it with `std::ref(obj)`. `callback` should be of type `std::function`. – jerry_fuyi Apr 20 '20 at 12:16
  • As you're using embedded C++11 (it's not EC++?), standard library may be unavailable. Just implement one in 1k lines of code. – jerry_fuyi Apr 20 '20 at 12:19

1 Answers1

0

I'm not sure I understand your question but, you cannot use a C++ class method as a callback function. More information here: How can I pass a class member function as a callback?

If you want to use method void test_callback(float arg); as a call back you need to define this method as a static method of the class and let the prototype of the callback "transport" the class instance it refers to.

Here is a modified version of your code you can use as a template:

class EXAMPLE_CLASS
{

public:
    /*
     * Standard Constructor
     */
    EXAMPLE_CLASS(){};
    float y;
    /*
     * Standard Destructor
     */
    virtual ~EXAMPLE_CLASS();


    static void test_callback(void* instance, float arg)
    {
        // retrieve the object
        auto obj = static_cast<EXAMPLE_CLASS*>(instance);
        obj->y = arg ;
    }

};
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
  • I edited the question to make more clear, what I want to do. There is a float callback function, that should call a member callback function with the same argument, such that member parameters can be adjusted. – HansPeterLoft Apr 20 '20 at 06:21
  • I modified my answer so that the code sample matches your updated question. If you want to update properties of the object: the property must be public (not private) and the callback method (test_callback) must be passed the arguments and an object callback instance. There is no other way around as far as I know – Jean-Marc Volle Apr 20 '20 at 08:32
  • That seems not to work. The function "void (*callback)(float arg);" is called several times and should lead to a valid callback function, where the value y is changed. In your example, the "void (*callback)(float arg);" does not point to any address. – HansPeterLoft Apr 20 '20 at 10:22
  • You also need to modify the callback pointer protocol so that it matches the class static method. `void (*callback)(void* instance,float arg);` Sorry I thought it was obvious based on the clarfication on how a class static method can be used as a callback – Jean-Marc Volle Apr 20 '20 at 10:26