模板、多态性、抽象基类指针和运行时转换

发布于 2024-10-30 18:20:03 字数 967 浏览 6 评论 0原文

我想在游戏中实现 Memento 模式,以实现检查点。

我有游戏中的 GameObject 指针列表。 GameObject 是一个抽象类,由 StaticObject、AnimatedObject 等类实现...

我想让我的 Memento 类尽可能抽象,所以我对整个 Memento 系统进行了模板化。

我的(非常准系统,用于调试目的)Memento 类是;

template<class T>
class Memento
{
public:
    Memento() { }

    Memento(T data)
    {
        setData(data);
    }
    void setData(T data)
    {
    //wanting this function to do complex behaviour
    }

    T _state;
};

稍后我会加入逻辑来区分 T 是否是指针。

_state 需要是实际 GameObject 副本的指针,而不仅仅是指针本身的副本,因为这违背了 Memento 的目的。

我想在 Memento 模板中执行的过程是:

1)传入抽象基类的指针。

2)确定它指向的子类的类型(在运行时)。

3)在堆上创建一个新的子类,其指针为_state(来自2中获得的类型)。

4)复制数据。

我遇到的问题是 3。我似乎无法获得子类的正确类型。

typeid(*data) 和 decltype(*data) 返回对子级的引用。如果我尝试使用 auto,它也是一个参考。显然我不能使用带有引用的 new 作为类型。

解决这个问题的方法显然是在 Memento 之外新建子类的副本并将其传递进去。但是,我很想知道是否有一种方法可以在运行时在 Template 类中完全完成此操作。我已经尝试了几个小时,看看能否让它发挥作用,而我的顽固部分却不想放弃。

I am wanting to implement a Memento pattern in a game for the purpose of implementing checkpoints.

I have a list of GameObject pointers in the game. GameObject is an abstract class, which is implemented by classes like StaticObject, AnimatedObject etc...

I would like to make my Memento class as abstract as possible, so I have templated my entire Memento system.

my (very barebones, for debugging purposes) Memento class is;

template<class T>
class Memento
{
public:
    Memento() { }

    Memento(T data)
    {
        setData(data);
    }
    void setData(T data)
    {
    //wanting this function to do complex behaviour
    }

    T _state;
};

I would put in logic to differentiate whether T is a pointer or not later.

_state needs to be a pointer of a copy of the actual GameObject, not just a copy of the pointer itself, as that defeats the purpose of a Memento.

The process of what I would like to do inside the Memento template is;

1) Pass in pointer to abstract base class.

2) Determine type of child class it is pointing to (at runtime).

3) Create a new child class on the heap, whose pointer is _state (from type obtained in 2).

4) Copy data.

The problem I am having is 3. There seems to be no way that I can get the proper type of the child class.

typeid(*data) and decltype(*data) return references to the children. If I try to use auto it is a reference as well. Obviously I can't use new with a reference as the type.

The way around this obviously would be to new the copy of the child class outside of Memento and pass that in. However, I would be interested to know if there is a way of doing it completely in the Template class at runtime. I've been trying to see if I can get it to work for a few hours, and the stubborn part of me does not want to give up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-11-06 18:20:03

您正在寻找的是“虚拟复制构造函数”,或克隆,习语。

class GameObject {
    ...
public:
    virtual GameObject* clone() = 0;
};

class StaticObject : public GameObject {
    ...
public:
    virtual StaticObject* clone() { return new StaticObject(*this); }
};

//..

What you're looking for is the "virtual copy constructor", or clone, idiom.

class GameObject {
    ...
public:
    virtual GameObject* clone() = 0;
};

class StaticObject : public GameObject {
    ...
public:
    virtual StaticObject* clone() { return new StaticObject(*this); }
};

//..
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文