在 C++ 中保存指向文件的指针

发布于 2024-10-06 00:38:28 字数 827 浏览 9 评论 0原文

我正在为学校的一门课程开发一款游戏。其中一项任务是将游戏保存到文件中,然后从同一文件加载游戏。

我遇到的问题是指针。我没有在堆栈上分配任何内容(例如,由于项目的所有权),因此所有类都有指向它们想要引用的任何内容的指针。

虽然这很容易解决(为每个对象分配一个 ID 并存储该 id 而不是指针),但真正的问题来自多重继承。

我们以 Actor 类为例。它看起来像这样: class Actor : public Object, public ItemOwner 其中 Object 是一个 Obj-C 风格的基类,它具有保留计数和释放、保留和自动释放方法。 ItemOwner 只是一个接口,它具有一些方法,例如 virtual bool add(Item *item) = 0;virtual void remove(Item *item) = 0; 和 < code>virtual bool transfer_ownership(Item *item, ItemOwner *new_owner) = 0;

现在真正的问题来了,哪个类(es?)应该有 ID。 Item 有一个指向 ItemOwner 的指针,而 Room 有一个指向 Actor 的指针。 演员只能被拯救一次。

我考虑过为每个超类(对象、ItemOwner 等)分配 ID,但如果我有一个指向 Actor 的指针,该 Actor 总是与它包含的对象具有相同的地址 (Actor *foo = new Actor( ); foo == (对象 *)foo)?如果不是游戏中的每个职业都需要有一个 ID。

任何想法或建议将不胜感激。

I'm developing a game for a course at my school. One of the assignments is to enable saving the game to a file and later load the game from the same file.

The problem I'm having are pointers. I'm not allocating anything on the stack (due to ownership of an item for example) so all the classes have pointers to whatever they want a reference to.

While this is quite easy to solve (assign an ID to each object and store that id instead of the pointer) the real problem comes with multiple inheritance.

Let's take the class Actor for example. It looks like this: class Actor : public Object, public ItemOwner where Object is a Obj-C-style base class which has a retain count and release, retain and autorelease methods. ItemOwner is simply an Interface which has some methods such as virtual bool add(Item *item) = 0;, virtual void remove(Item *item) = 0; and virtual bool transfer_ownership(Item *item, ItemOwner *new_owner) = 0;

Now the real question comes, which class(es?) should have ID's. And Item has a pointer to an ItemOwner while a Room has a pointer to an Actor.
The actor should only be saved once.

I've thought about assigning ID's to each superclass (Object, ItemOwner, etc) but if I have a pointer to an Actor will that actor always have the same adress as the Object it contains (Actor *foo = new Actor(); foo == (Object *)foo)? If not every single class in the game will need to have an ID.

Any thoughts or suggestions would be greatly appreciated.

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

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

发布评论

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

评论(3

萌无敌 2024-10-13 00:38:29

通过使用 UID,您可以创建映射:

serialized_object -> UID -> deserialized_object.

为什么要麻烦呢?您已经有了 UID,它们是指向对象的指针。通过使用此映射:

&serialized_object -> &deserialized_object

您可以删除自动创建的间接级别、UID,您所要做的就是在反序列化过程中推导此映射。

最简单的方法是按原样使用所有指针序列化对象,并将每个对象的地址存储在一起。这也将帮助您检查对象是否已经序列化。

虽然序列化很简单,但反序列化有其技巧。您必须小心地用正确的对象地址更新每个旧指针(还记得吗?它们包含不再有效的地址)。

By using UIDs, you create a mapping:

serialized_object -> UID -> deserialized_object.

Why bothering? You already have UIDs, and these are pointers to objects. By using this mapping:

&serialized_object -> &deserialized_object

you drop a level of indirection, UIDs as created automatically, and all you have to do is to deduce this mapping in deserealization process.

The simpliest way is to serialize the objects with all the pointers as is, and to store together with each object its address. This will also help you check if an object was serialized alraedy.

While serialization would be simple, deserialization will have its tricks. You'll have to be carefull to update each old pointer (remember? they contained adresses that are no more valid) with the correct object address.

谁与争疯 2024-10-13 00:38:29

一种选择是定义一个类,其生命目的是保存一个 ID,并使其成为您打算保存到磁盘的每个类的虚拟基类。

One option is to define a class who's purpose in life is to hold an ID, and make it a virtual base class of every class you intend to save to disk.

暖树树初阳… 2024-10-13 00:38:29

我会为每个对象放置一个实例,对于演员来说,对象和 ItemOwner 的 ID 应该相同,因为它们都是相同的实例。

另外,您可以考虑使用处理程序,而不是使用指针,如下所述: http://gamesfromwithin.com/管理数据关系

I would put a single per object instance, for the actor, its ID should be the same for the Object and ItemOwner, because they are all the same instance.

Also, instead of using pointers you can think about using handlers, like described here: http://gamesfromwithin.com/managing-data-relationships

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