类和子类中的C ++成员函数指针 [英] C++ member function pointers in class and subclass

查看:124
本文介绍了类和子类中的C ++成员函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类,它拥有映射用于像这样的函数指针

I have one base class which holds a map for function pointers like this

typedef void (BaseClass::*event_t)();
class BaseClass {
    protected:
        std::map<std::string, event_t> events;
    public:
        // Example event
        void onFoo() {
            // can be added easily to the map
        }
};

处理这个工作完成,但现在我想让 BaseClass 一个抽象基类从这样派生:

Handling this works prefect, but now i want to make BaseClass an abstract base class to derive from like this:

 class SpecificClass : public BaseClass {
     public:
         void onBar() {
             // this is gonna be difficult!
         }
 };


$ b <我不能添加 onBar ,因为 event_t 类型只定义为 BaseClass !是否有任何可能性(也许有模板?),它不会导致定义 event_t 每个类,我会使用...

Although i can access the map from SpecificClass i am not able to add onBar because the event_t type is only defined for the BaseClass! Is there any possibility (maybe with templates?) which does not lead to define the event_t for each class i will use...

(这不是必要的使用模板!任何好/合适的方法会很好。)

(It is not neccessary to use templates! Any good/suitable approach would be nice.)

更多背景信息:

这整个事情是基于文本的RPG。我的基类可以调用 Location 和指定一个任何位置。 CivicCenter 。每个 Location 对象订阅我的 EventSystem ,当我触发一个事件时,它通知所有必要的对象。因此,我想在地图中存储一些指向私有函数的指针,这些函数用他们的名称像 onSetOnFire (xD)作为键来保存操作。

This whole thing is for a text based RPG. My base class could be called Location and the specifc one any location e.g. CivicCenter. Each Location object subscribes to my EventSystem which notifies all neccessary objects when i fire an event. Therefore i want to store in a map some pointers to private functions holding the actions with their "name" like onSetOnFire (xD) as the key.

推荐答案

经过一些思考和重新设计,我能够实现我想要的。虽然我固执,仍然使用继承我重新实现了地图。这是现在的工作方式:

After some thought and a redesign i was able to achieve what i wanted. Although i am stubborn and still using inheritance i have reimplemented the map. This is how it works now:

class Location {
    // ...

    protected:
        std::map<std::string, std::function<void(void)>> m_mEvents;
};

现在我可以这样处理:

class CivicCenter : public Location {
    public:
        CivicCenter() {
            // this is done by a macro which lookes better than this
            this->m_mEvents["onTriggerSomething"] =
                  std::bind(&CivicCenter::onTriggerSomething, this);
        }

        void onTriggerSomething() {
            // ...
        }

    // ...
};

轻松使用 std :: bind 我能够实现通用函数指针。当使用参数 std :: function< void(int,int)> remeber使用boost的 _1 _2 或像我这样的lambda表达式:

With easy use of std::bind i am able to implement generic function pointers. When using parameters like in std::function<void(int, int)> remeber to use either boost's _1 and _2 or lambda expressions like me:

std::function<void(int,int)> f = [=](int a, int b) {
    this->anotherFunctionWithParams(a, b);
};

但这只是因为我的解决方案的完整性而指出。

But this is just pointed out due to completeness of my solution.

这篇关于类和子类中的C ++成员函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆