Простые примеры обратного вызова членов класса C++

Я знаю, что об этом спрашивали очень много раз, и из-за этого трудно копаться в луже и найти простой пример того, что работает.

У меня есть это, это просто, и это работает для MyClass...

#include <iostream>
using std::cout;
using std::endl;

class MyClass
{
    public:
        MyClass();
        static void Callback(MyClass* instance, int x);
    private:
        int private_x;
};

class EventHandler
{
    public:
        void addHandler(MyClass* owner)
        {
            cout << "Handler added..." << endl;
            //Let's pretend an event just occured
            owner->Callback(owner,1);
        }
};

EventHandler* handler;

MyClass::MyClass()
{
    private_x = 5;
    handler->addHandler(this);
}

void MyClass::Callback(MyClass* instance, int x)
{
    cout << x + instance->private_x << endl;
}

int main(int argc, char** argv)
{
    handler = new EventHandler();
    MyClass* myClass = new MyClass();
}

class YourClass
{
    public:
        YourClass();
        static void Callback(YourClass* instance, int x);
};

Как это можно переписать так EventHandler::addHandler() будет работать с обоими MyClass а также YourClass, Извините, но так работает мой мозг, мне нужно увидеть простой пример того, что работает, прежде чем я смогу понять, почему / как он работает. Если у вас есть любимый способ заставить эту работу работать, сейчас самое время показать это, пожалуйста, разметьте этот код и отправьте его обратно.

[редактировать]

Ответ был получен, но ответ был удален, прежде чем я смог поставить галочку. Ответ в моем случае был шаблонной функцией. Поменял addHandler на этот...

class EventHandler
{
    public:
        template<typename T>
        void addHandler(T* owner)
        {
            cout << "Handler added..." << endl;
            //Let's pretend an event just occured
            owner->Callback(owner,1);
        }
};

6 ответов

Решение

Вместо статических методов и передачи указателя на экземпляр класса вы можете использовать функциональность в новом стандарте C++11: std::function а также std::bind:

#include <functional>
class EventHandler
{
    public:
        void addHandler(std::function<void(int)> callback)
        {
            cout << "Handler added..." << endl;
            // Let's pretend an event just occured
            callback(1);
        }
};

addHandler метод теперь принимает std::function аргумент, и этот "объект функции" не имеет возвращаемого значения и принимает целое число в качестве аргумента.

Чтобы привязать его к определенной функции, вы используете std::bind:

class MyClass
{
    public:
        MyClass();

        // Note: No longer marked `static`, and only takes the actual argument
        void Callback(int x);
    private:
        int private_x;
};

MyClass::MyClass()
{
    using namespace std::placeholders; // for `_1`

    private_x = 5;
    handler->addHandler(std::bind(&MyClass::Callback, this, _1));
}

void MyClass::Callback(int x)
{
    // No longer needs an explicit `instance` argument,
    // as `this` is set up properly
    cout << x + private_x << endl;
}

Вам нужно использовать std::bind при добавлении обработчика, поскольку вам явно необходимо указать неявное в противном случае this указатель в качестве аргумента. Если у вас есть отдельно стоящая функция, вам не нужно использовать std::bind:

void freeStandingCallback(int x)
{
    // ...
}

int main()
{
    // ...
    handler->addHandler(freeStandingCallback);
}

Использование обработчика событий std::function объекты, также позволяет использовать новые лямбда-функции C++11:

handler->addHandler([](int x) { std::cout << "x is " << x << '\n'; });

Вот краткая версия, которая работает с обратными вызовами методов класса и с обычными обратными вызовами функций. В этом примере, чтобы показать, как обрабатываются параметры, функция обратного вызова принимает два параметра: bool а также int,

class Caller {
  template<class T> void addCallback(T* const object, void(T::* const mf)(bool,int))
  {
    using namespace std::placeholders; 
    callbacks_.emplace_back(std::bind(mf, object, _1, _2));
  }
  void addCallback(void(* const fun)(bool,int)) 
  {
    callbacks_.emplace_back(fun);
  }
  void callCallbacks(bool firstval, int secondval) 
  {
    for (const auto& cb : callbacks_)
      cb(firstval, secondval);
  }
private:
  std::vector<std::function<void(bool,int)>> callbacks_;
}

class Callee {
  void MyFunction(bool,int);
}

//then, somewhere in Callee, to add the callback, given a pointer to Caller `ptr`

ptr->addCallback(this, &Callee::MyFunction);

//or to add a call back to a regular function
ptr->addCallback(&MyRegularFunction);

Это ограничивает специфичный для C++11 код методом addCallback и частными данными в классе Caller. Для меня, по крайней мере, это сводит к минимуму вероятность ошибок при его реализации.

Полный рабочий пример из кода выше.... для C++11:

#include <stdlib.h>
#include <stdio.h>
#include <functional>

#if __cplusplus <= 199711L
  #error This file needs at least a C++11 compliant compiler, try using:
  #error    $ g++ -std=c++11 ..
#endif

using namespace std;

class EventHandler {
    public:
        void addHandler(std::function<void(int)> callback) {
            printf("\nHandler added...");
            // Let's pretend an event just occured
            callback(1);
        }
};


class MyClass
{
    public:
        MyClass(int);
        // Note: No longer marked `static`, and only takes the actual argument
        void Callback(int x);

    private:
        EventHandler *pHandler;
        int private_x;
};

MyClass::MyClass(int value) {
    using namespace std::placeholders; // for `_1`

    pHandler = new EventHandler();
    private_x = value;
    pHandler->addHandler(std::bind(&MyClass::Callback, this, _1));
}

void MyClass::Callback(int x) {
    // No longer needs an explicit `instance` argument,
    // as `this` is set up properly
    printf("\nResult:%d\n\n", (x+private_x));
}

// Main method
int main(int argc, char const *argv[]) {

    printf("\nCompiler:%ld\n", __cplusplus);
    new MyClass(5);
    return 0;
}


// where $1 is your .cpp file name... this is the command used:
// g++ -std=c++11 -Wall -o $1 $1.cpp
// chmod 700 $1
// ./$1

Выход должен быть:

Compiler:201103

Handler added...
Result:6

Что вы хотите сделать, это создать интерфейс, который обрабатывает этот код, и все ваши классы реализуют этот интерфейс.

class IEventListener{
public:
   void OnEvent(int x) = 0;  // renamed Callback to OnEvent removed the instance, you can add it back if you want.
};


class MyClass :public IEventListener
{
    ...
    void OnEvent(int x); //typically such a function is NOT static. This wont work if it is static.
};

class YourClass :public IEventListener
{

Обратите внимание, что для того, чтобы это работало, функция "обратного вызова" не является статичной, что, я считаю, является улучшением. Если вы хотите, чтобы он был статичным, вам нужно сделать это, как рекомендует JaredC с шаблонами.

MyClass а также YourClass оба могут быть получены из SomeonesClass который имеет абстрактный (виртуальный) Callback метод. Ваш addHandler будет принимать объекты типа SomeonesClass а также MyClass а также YourClass может переопределить Callback обеспечить их конкретную реализацию поведения обратного вызова.

Если у вас есть обратные вызовы с различными параметрами, вы можете использовать шаблоны следующим образом:
// скомпилировать с: g++ -std= C++11 myTemplatedCPPcallbacks.cpp -o myTemplatedCPPcallbacksApp

#include <functional>     // c++11

#include <iostream>        // due to: cout


using std::cout;
using std::endl;

class MyClass
{
    public:
        MyClass();
        static void Callback(MyClass* instance, int x);
    private:
        int private_x;
};

class OtherClass
{
    public:
        OtherClass();
        static void Callback(OtherClass* instance, std::string str);
    private:
        std::string private_str;
};

class EventHandler
{

    public:
        template<typename T, class T2>
        void addHandler(T* owner, T2 arg2)
        {
            cout << "\nHandler added..." << endl;
            //Let's pretend an event just occured
            owner->Callback(owner, arg2);
         }   

};

MyClass::MyClass()
{
    EventHandler* handler;
    private_x = 4;
    handler->addHandler(this, private_x);
}

OtherClass::OtherClass()
{
    EventHandler* handler;
    private_str = "moh ";
    handler->addHandler(this, private_str );
}

void MyClass::Callback(MyClass* instance, int x)
{
    cout << " MyClass::Callback(MyClass* instance, int x) ==> " 
         << 6 + x + instance->private_x << endl;
}

void OtherClass::Callback(OtherClass* instance, std::string private_str)
{
    cout << " OtherClass::Callback(OtherClass* instance, std::string private_str) ==> " 
         << " Hello " << instance->private_str << endl;
}

int main(int argc, char** argv)
{
    EventHandler* handler;
    handler = new EventHandler();
    MyClass* myClass = new MyClass();
    OtherClass* myOtherClass = new OtherClass();
}
Другие вопросы по тегам