具有向下转换函数指针的类层次结构

时间:2017-11-07 20:40:22

标签: c++ abstract-class member-function-pointers virtual-inheritance incompatibletypeerror

TL; DR 如何让Base::Choose接受Derived::CustomUserFunction作为参数? 我得到的错误,我明白为什么会得到它,但我不知道如何解决它是: Argument of type "Derived::* (...)" is incompatible with parameter of type "Base::* (...)"

#include <iostream>
    class Base {
    public:
        void Base::Choose(void(Base::*FctPtr)()) {
            (this->*FctPtr)();
        }

        virtual void Base::CustomUserFunction() = 0;
};

    class Derived : public Base {
    public:
        virtual void Derived::CustomUserFunction() {
            std::cout << "CustomFunction\n";
        }
 };

    int main()  {
        Derived D;
        D.Choose(&D.CustomUserFunction);
    }

完整版

我正在编写一个使用自定义用户功能的Discreet事件模拟引擎(DESEngine)。由于这些自定义用户功能特定于问题,我希望能够在不知道其签名名称的情况下将其称为引擎会在文本文件中读取TURN_ON并且查找映射到unordered_map中TURN_ON字符串别名的函数指针。

基本上,DESEngine会调用UserEvents::Choose(std::string Name)来调用它的事件名称(字符串)给出的函数。

一切都运行良好,但可管理性变得越来越难,因为我必须使用引擎来解决许多不同的问题(从微分方程求解到模拟简单的计算机)。 (我在UserEvents类中有各种不同的特定于问题的函数)

因此,我决定让我的班级UserEvents成为一个抽象类,为我遇到的每种问题继承它,因此,自定义用户函数将存储在派生类中class ,不在基础中,但我希望"Choose"方法驻留在基类中。 如何从基类中调用派生类中的函数?

我得到的错误,我明白为什么会得到它,但我不知道如何解决它是: Argument of type "UserEvents_MVN::* (...)" is incompatible with parameter of type "UserEvents::* (...)"

class UserEvents
{   
    public:
    // Receives an event name and parameters
    struct EventWithParams { std::string Name; std::vector<boost::any> Params; };

    // Lets an outside class (DESEngine) configure which event to run before calling UserEvents::Choose()
    // TODO: Make getters/setters so Events can be passed through a better way
        EventWithParams Event;

        // Select which function(parameters) to call based on the EventWithParams Event (above)
        int UserEvents::Choose();

    protected:
        void UserEvents::UserFunctionPointerMap_AddFunction
        (std::string Alias, void(UserEvents::*FunctionPointer)(const std::vector<boost::any>&));

        virtual void UserEvents::BuildUFPAliasMap() = 0;

    private:

        // Here we have an unordered map that assigns User Function (pointer) to each Key (string or Alias or Event Name)
        std::unordered_map<std::string, void(UserEvents::*)(const std::vector<boost::any>&)> UserFunctionPointerAliasMap;
    };

这里是选择

的实现
int UserEvents::Choose()
    {
        try
        {
            (this->*UserFunctionPointerAliasMap.at(Event.Name))(Event.Params);
            return 0;
        }
        catch (const std::out_of_range e)
        {
            throw std::exception::exception("Unknown User Event");
            return -1;
        }
    }

派生类的一个例子

#pragma once
#include "..\\UserEvents.h"

class UserEvents_MVN : public UserEvents
{
public:
    UserEvents_MVN();
    ~UserEvents_MVN();

protected:

    void UserEvents_MVN::MEMDUMP_LOAD(const std::vector<boost::any> &Parameters);

    void UserEvents_MVN::MEMDUMP(const std::vector<boost::any> &Parameters);

    void UserEvents_MVN::InstructionDecode(const std::vector<boost::any> &Parameters);


private:

    virtual void UserEvents_MVN::BuildUFPAliasMap();
};

我如何构建函数指针索引(UserFunctionPointerAliasMap)

void UserEvents_MVN::BuildUFPAliasMap()
{
    UserFunctionPointerMap_AddFunction("MEMDUMP_LOAD",  &UserEvents_MVN::MEMDUMP_LOAD );
    UserFunctionPointerMap_AddFunction("MEMDUMP",   &UserEvents_MVN::MEMDUMP );
    UserFunctionPointerMap_AddFunction("DECODE",    &UserEvents_MVN::InstructionDecode);
}

2 个答案:

答案 0 :(得分:0)

解决问题的一种方法是在virtual中定义UserEvents成员函数,并在构建地图时使用它们。

示例:

class UserEvents
{   
  public:

    virtual void MEMDUMP_LOAD(const std::vector<boost::any> &Parameters) = 0;

 ...
};

然后覆盖virtual中的UserEvents_MVN功能区。

class UserEvents_MVN : public UserEvents
{
  public:
    UserEvents_MVN();
    ~UserEvents_MVN();

  protected:

    void MEMDUMP_LOAD(const std::vector<boost::any> &Parameters) override;

  ...
};

使用以下方法构建地图:

void UserEvents_MVN::BuildUFPAliasMap()
{
    UserFunctionPointerMap_AddFunction("MEMDUMP_LOAD",  &UserEvents::MEMDUMP_LOAD );
    ...
}

更新,以回应OP的评论

另一种选择是在地图中使用std::function

假设您的map定义为:

using FunctionMap = std::map<std::string, std::function<void(UserEvent*, std::vector<boost::any>&)>>;
FunctionMap myFunctionMap;

然后,您可以使用lambda函数充实myFunctionMap

void UserEvents_MVN::BuildUFPAliasMap()
{
    UserFunctionPointerMap_AddFunction("MEMDUMP_LOAD",
                                        [](UserEvent* event, std::vector<boost::any>& parameters))
                                        { dynamic_cast<UserEvents_MVN*>(event)->MEMDUMP_LOAD(parameters);});
    ...
}

答案 1 :(得分:0)

我在IRC寻求帮助,有人向我指出CRTP - Curiously Recurring Template Pattern允许Base内的方法访问Derived的成员,这就是我的目标。

// The Curiously Recurring Template Pattern (CRTP)
template<class T>
class Base
{
    // methods within Base can use template to access members of Derived
};
class Derived : public Base<Derived>
{
    // ...
};

他们给我发了example

#include <string>
#include <unordered_map>
#include <iostream>

template <typename T>
class Dispatch
{
    std::unordered_map<std::string, void (T::*)()> commands;

    protected: // used by application type only
    void Add(std::string name, void (T::*fptr)())
    {
        commands[name] = fptr;
    }

    public: // becomes part of the public interface for application type
    /* This part is the only reason to use CRTP/inheritance. The application
       type could simply create this method itself and just call functions of
       a Dispatch<App> member to do all the work, but this approach saves us
       from typing that out. */
    void Choose(std::string s)
    {
        auto fptr = commands.at(s);
        auto target = static_cast<T*>(this);
        (target->*fptr)();
    }
};

class App : public Dispatch<App>
{
public:
    App()
    {
        // for demo purposes
        Add("foo", &App::Test);
    }

    void Test()
    {
        std::cout << "dispatched!\n";
    }
};

int main()
{
    App a;
    a.Choose("foo");
}