Z
Z
Zaur Abdulgalimov2015-09-23 19:23:07
C++ / C#
Zaur Abdulgalimov, 2015-09-23 19:23:07

How to pass a function as a parameter to another function?

Coconut has an ActionFloat class, and it has a function:

static ActionFloat* create(float duration, float from, float to, ActionFloatCallback callback);

The ActionFloatCallback type is declared like this:
typedef std::function<void(float value)> ActionFloatCallback;

How can I pass a link to the method of my class in the class constructor?
MyClass::MyClass()
{
   ActionFloat* floatAction = ActionFloat::create(0.5f, 0, 1, this->callback); // так не работает
   //ActionFloat* floatAction = ActionFloat::create(0.5f, 0, 1, testFunc); // так работает
}

void MyClass::callback(float t)
{
    CCLOG("test: %f", t);
}

void testFunc(float t)
{
    CCLOG("test: %f", t);
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya, 2015-09-23
@abdulgalimov

For example with a lambda:

MyClass()
{
     ActionFloat * action = create(0.5f, 0, 1, [this](float arg) {callback(arg); });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question