gpt4 book ai didi

c++ - 在抽象类之间传递数据

转载 作者:行者123 更新时间:2023-11-28 07:40:25 24 4
gpt4 key购买 nike

我正在寻找有关完成我将要描述的内容的最佳 OO 方法的意见。我正在编写将成为游戏等事件系统的东西,我希望它尽可能可扩展,因为有很多抽象类。其中两个是被分配来监视一个事件的监视器,以及回调函数,它包装了事件发生时要调用的函数指针。当我想发送回调需要的数据时,问题就出现了。将发送的数据将是特定于子类的(取决于函数签名)并存储在子类监视器中。我希望能够在调用执行之前将这些数据传递给回调,但是由于从监视器的角度来看一切都是抽象的,所以这很困难。我正在寻找有关如何以最佳 OO 方式执行此操作的建议,到目前为止我还没有想出任何我太喜欢的东西。由于回调被发送到另一个类以实际分派(dispatch),因此数据需要在某个时候结束在它们内部。

供引用,监视器抽象类

#pragma once

#include "DIVE_GUI_Types.h"
#include "DIVE_GUI_Callback.h"
#include "DIVE_GUI_Event_Dispatcher.h"
#include <map>
#include <string>

/*
Class to monitor events to be handled by the event system.
*/

class DIVE_GUI_Event_Monitor
{
private:
friend class DIVE_GUI_Kernel;
DIVE_GUI_Event_Dispatcher* m_Dispatcher;
static DIVE_HANDLE m_Active_GUI;

protected:
const std::string m_Event_ID;
std::map<DIVE_HANDLE, DIVE_GUI_Callback*> m_GUI_Map;
virtual bool Dispatch() = 0;

public:
void Update();
std::string Get_Event_ID() const { return m_Event_ID; }
DIVE_GUI_Event_Monitor(const std::string& id) : m_Event_ID(id) { }
void Add_Callback(DIVE_HANDLE, DIVE_GUI_Callback* function);
};

和回调抽象类

#pragma once

/*
Abstract class representing a wrapper for a callback function as per the Command design pattern.
*/

class DIVE_GUI_Callback
{
public:
virtual void Excecute_Callback() const = 0;
};

任何和所有意见/建议都将受到赞赏。谢谢!

最佳答案

如果我对您的理解正确,则应将此数据提供给回调构造函数。假设您有从 DIVE_GUI_Callback 派生的 Callback1Callback2。所以代码看起来像:

DIVE_GUI_Event_Monitor* monitor; 
monitor->Add_Callback(Callback1(specific_data_1));
monitor->Add_Callback(Callback2(specific_data_2));

此特定数据将在 Excecute_Callback() 中使用。

关于c++ - 在抽象类之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15935878/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com