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

Class Hierarchy with down cast function pointers

本文关键字:指针 函数 层次结构 转换      更新时间:2023-10-16

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 << "CustomFunctionn";
}
};
int main()  {
Derived D;
D.Choose(&D.CustomUserFunction);
}

完整版

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

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

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

正因为如此,我决定将我的类UserEvents抽象类,并为我遇到的每种问题继承它,因此,自定义用户函数将存储在派生类中,而不是基类中,但我希望"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);
}

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

例:

class UserEvents
{   
public:
virtual void MEMDUMP_LOAD(const std::vector<boost::any> &Parameters) = 0;
...
};

然后覆盖virtualUserEvents_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);});
...
}

我在IRC中寻求帮助,有人向我指出CRTP - 奇怪的重复模板模式,它允许Base中的方法访问派生的成员,这就是我的目标。

// 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>
{
// ...
};

他们好心地给我发了一个例子

#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");
}