C++ 在C+中使用类模板作为回调+;

C++ 在C+中使用类模板作为回调+;,c++,class,templates,c++03,C++,Class,Templates,C++03,我能够使回调在没有类模板的情况下工作。但我的要求是通过传递模板形式的类对象实现回调。我已经把我的意图写在了主体()中,但不知何故,我无法使它起作用 我不能使用boost和C++11来解决当前问题。任何帮助都将不胜感激 // TestProj.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "glb.h" #include "Test.h" #include &l

我能够使回调在没有类模板的情况下工作。但我的要求是通过传递模板形式的类对象实现回调。我已经把我的意图写在了主体()中,但不知何故,我无法使它起作用

我不能使用boost和C++11来解决当前问题。任何帮助都将不胜感激

// TestProj.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "glb.h"
#include "Test.h"
#include <iostream>
using namespace std;

class MyCallBack
{
private:
    class Callback
    {
    public:
        virtual ~Callback() { }
        virtual void call() = 0;
    };

    template <typename T>
    class ClassCallback : public Callback
    {
    private:
        T*      object;
        void (T::*callback)();

    public:
        ClassCallback(T* obj, void (T::*clbk)()) : object(obj), callback(clbk) {}
        virtual void call() { (object->*callback)(); }
    };

private:
    Callback*       callback;

public:
    MyCallBack() : callback(NULL) { }
    ~MyCallBack() { delete callback; }

    template <typename T>
    MyCallBack(T* obj, void (T::*clbk)())
    {
        callback = new ClassCallback<T>(obj,clbk);
    }

    void operator () ()
    {
        callback->call();
    }
};

typedef enum {
    EVENT1 = 1,
    EVENT2 = 2,
    EVENT3 = 4,
    EVENT4 = 8
} MyEvent_t;

template <class EventT> 
class EventHandler
{
public:
    virtual void on_event(EventT _event) = 0;
};

class MyHandler:public EventHandler<MyEvent_t>{

    virtual void on_event(MyEvent_t _event){

        switch(_event){

            case EVENT1:
                break;

            case EVENT2:
                break;

        }
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    EventHandler<MyEvent_t> *my_handler = new MyHandler();
    MyCallBack rcb(my_handler,&MyHandler<MyEvent_t>::on_event);

    // to check the callback
    rcb();

    return 0;
}
//TestProj.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“glb.h”
#包括“Test.h”
#包括
使用名称空间std;
类MyCallBack
{
私人:
类回调
{
公众:
虚拟~Callback(){}
虚空调用()=0;
};
模板
类ClassCallback:公共回调
{
私人:
T*对象;
无效(T::*回调)();
公众:
类回调(T*obj,void(T::*clbk)():对象(obj),回调(clbk){}
虚拟无效调用(){(对象->*回调)(;}
};
私人:
回调*回调;
公众:
MyCallBack():回调(NULL){}
~MyCallBack(){delete callback;}
模板
MyCallBack(T*obj,void(T::*clbk)()
{
回调=新类回调(obj,clbk);
}
void运算符()()
{
回调->调用();
}
};
类型定义枚举{
事件1=1,
事件2=2,
事件3=4,
事件4=8
}MyEvent_t;
模板
类事件处理程序
{
公众:
事件上的虚拟无效(事件T事件)=0;
};
类MyHandler:公共事件处理程序{
事件上的虚拟无效(MyEvent\u t\u事件){
开关(_事件){
案例事件1:
打破
案例事件2:
打破
}
}
};
int _tmain(int argc,_TCHAR*argv[]
{
EventHandler*my_handler=new MyHandler();
MyCallBack rcb(my_处理程序,&MyHandler::on_事件);
//检查回调
rcb();
返回0;
}
感谢所有的线索

线路

MyCallBack rcb(my_handler,&MyHandler<MyEvent_t>::on_event);
线路

MyCallBack rcb(my_handler,&MyHandler<MyEvent_t>::on_event);

我认为你想要达到的是行动模式。 请看这里:

现在谈谈实施:

#include <iostream>
#include <memory>

struct Action {
    virtual void execute() = 0;
};

struct SomeAction : public Action {
    void execute() {
        std::cout << "SomeAction" << std::endl;
    }
};

struct SomeOtherAction : public Action {
    void execute() {
        std::cout << "SomeOtherAction" << std::endl;
    }
};

class EventHandler {

    std::unique_ptr<Action> action; // The unique ptr will delete action if the event handler is destroyed

public:
    EventHandler() :
        action(new SomeAction())
    {

    }
    void setAction(Action* newAction) {
        action = std::unique_ptr<Action>(newAction);
    }
    void eventCalled() {
        // This function is invoked from the outside if the event happens
        action->execute();
    }
};

int main() {
    EventHandler eventHandler;

    //Some event is called
    eventHandler.eventCalled(); // Output "SomeAction"


    // We change the action later in time
    eventHandler.setAction(new SomeOtherAction());
    // Another Event gets called
    eventHandler.eventCalled(); // Output "SomeOtherAction"
    return 0;
}
#包括
#包括
结构动作{
虚空执行()=0;
};
struct SomeAction:公共操作{
void execute(){

我想你想要达到的是行动模式。 请看这里:

现在谈谈实施:

#include <iostream>
#include <memory>

struct Action {
    virtual void execute() = 0;
};

struct SomeAction : public Action {
    void execute() {
        std::cout << "SomeAction" << std::endl;
    }
};

struct SomeOtherAction : public Action {
    void execute() {
        std::cout << "SomeOtherAction" << std::endl;
    }
};

class EventHandler {

    std::unique_ptr<Action> action; // The unique ptr will delete action if the event handler is destroyed

public:
    EventHandler() :
        action(new SomeAction())
    {

    }
    void setAction(Action* newAction) {
        action = std::unique_ptr<Action>(newAction);
    }
    void eventCalled() {
        // This function is invoked from the outside if the event happens
        action->execute();
    }
};

int main() {
    EventHandler eventHandler;

    //Some event is called
    eventHandler.eventCalled(); // Output "SomeAction"


    // We change the action later in time
    eventHandler.setAction(new SomeOtherAction());
    // Another Event gets called
    eventHandler.eventCalled(); // Output "SomeOtherAction"
    return 0;
}
#包括
#包括
结构动作{
虚空执行()=0;
};
struct SomeAction:公共操作{
void execute(){

std::cout我相信我们可以帮助您,如果您能更具体地说明问题,和/或减少代码。对于初学者,
on_event
期望
event
。您的
无效(T:::*回调)()嗨,巴雷特,是的,我知道这个论点没有了。我的问题是如何添加它。我是C++和模板的新手。MyCallBack rcb也是MyHuffer-MyHand::OnIAL事件。;具有RS指出的类型。应该是事件处理程序而不是Myhandler。我相信我们可以帮助您,如果您能更具体地说明问题,和/或减少代码。对于初学者,
on_Event
期望
Event
。您的
无效(T::*回调)()嗨,巴雷特,是的,我知道这个论点没有了。我的问题是如何添加它。我是C++和模板的新手。MyCallBack rcb也是MyHuffer-MyHand::OnIAL事件。;具有RS指出的类型。应为事件处理程序,而不是Myhandler。如果您不清楚如何从事件映射到操作(或可能的多个操作),请随时发表评论。如果您不清楚如何从事件映射到操作(或可能的多个操作),请随时发表评论。