类变量在其定义范围内?

发布于 2024-09-29 06:08:23 字数 279 浏览 11 评论 0原文

这可能是一个愚蠢的问题。我正在尝试制作文本泥。我需要每个 Room 类包含其他 Room 类,以便在尝试移动到它们或从它们获取信息时可以引用。但是,我不能这样做,因为我显然无法在其定义中声明一个类。那么,我该怎么做呢?当我说我做不到时,我的意思是:

class Room {
    public:
        Room NorthRoom;
        Room EastRoom;
        Room SouthRoom;
        Room WestRoom;
};

This is probably a dumb question. I am trying to make a text-mud. I need each Room class to contain other Room classes that one can refer to when trying to move to them or get information from them. However, I can not do that because I obviously can not declare a class within its definition. So, how do I do this? Here's what I mean when I state I can not do it:

class Room {
    public:
        Room NorthRoom;
        Room EastRoom;
        Room SouthRoom;
        Room WestRoom;
};

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

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

发布评论

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

评论(4

东京女 2024-10-06 06:08:23

不可能有 Room 成员变量。不过,您可以使用指针或引用。

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

It's not possible to have a Room member variable. You could use a pointer or reference though.

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};
情绪少女 2024-10-06 06:08:23

我确信不是每个房间都有四个儿童房,对吧?否则,你的房间数量是无限的,这在有限的内存中很难处理:-)

你可以尝试

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

然后,当房间没有孩子时,你可以拥有 NULL 指针。

I am sure not EVERY room has four children rooms, right? Otherwise the number of your rooms is infinity which is hard to handle in finite memory :-)

You might try

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

Then you can have NULL pointers when a room doesn't have children.

爱殇璃 2024-10-06 06:08:23

您的 Room 需要有指向其他 Room(即 Room*)的指针

类类型对象(如 Room)的大小至少足以容纳其所有成员变量(因此,如果将其每个成员变量的大小相加,您将得到类的最小大小。

如果一个类可以包含其自己类型的成员变量,那么它的大小将是无限的(每个 Room 包含四个其他 Room,每个其中包含其他四个 Room,每个房间都包含...)。

C++ 没有像 Java 和 C# 那样的引用类型对象。

Your Room needs to have pointers to other Rooms (that is, Room*s).

A class type object (like Room) has a size that is at least large enough to contain all its member variables (so, if you add up the sizes of each of its member variables, you'll get the smallest size that the class can be.

If a class could contain member variables of its own type then its size would be infinite (each Room contains four other Rooms, each of which contains four other Rooms, each of which contains...).

C++ doesn't have reference type objects like Java and C#.

难如初 2024-10-06 06:08:23

您应该使用指针:

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

可能的原因是类还没有其构造函数,因此当您使用指针时,当类具有构造函数定义时,您会稍后初始化它们。

You should use pointers:

class Room {
    public:
        Room* NorthRoom;
        Room* EastRoom;
        Room* SouthRoom;
        Room* WestRoom;
};

Probably cause is that class does not yet its constructor, so when you use pointers you init them later, when class has construcotr definition.

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