C++构造函数参数问题

发布于 2024-09-04 09:56:10 字数 542 浏览 6 评论 0原文

我正在学习C++。我有一个名为 GameContext 的简单类:

class GameContext {
    public:
        GameContext(World world);
        virtual ~GameContext();
};

要初始化 GameContext 对象,我需要一个 World 对象。

  • 如果 GameContext 构造函数采用指向 World 对象 (World*) 的指针,则为 World 对象的地址 (&World )还是对 World 对象(World)的引用

  • 在参数附近使用时,const 关键字是什么?例如: GameContext(const World &world)

谢谢。

I'm learning C++. I have a simple class named GameContext:

class GameContext {
    public:
        GameContext(World world);
        virtual ~GameContext();
};

To initialize a GameContext object, I need a World object.

  • Should the GameContext constructur take a pointer to a World object (World*), the address to a World object (&World) or a reference to a World object (World)?

  • What is the const keyword when used near a parameter? For example: GameContext(const World &world)

Thanks.

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

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

发布评论

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

评论(7

原谅过去的我 2024-09-11 09:56:10

首先,术语:World * 是指向 World 对象的指针,这是正确的。然而,World & 将是对 World 对象的引用。 World 是 World 对象的副本,而不是对其的引用。

const(主要与指针或引用一起使用,如 World const &worldWorld const *world)意味着您获取对 const 对象的引用/指针——换句话说,您不允许修改它引用/指向的原始对象。

对于小对象,您通常希望传递一个副本。对于大型对象,您通常需要传递 const 引用。虽然也有例外,但这是一个合理的经验法则,直到您了解了足够多的知识以知道何时打破规则(可以这么说)。

仅根据名称,我猜测您的 World 对象可能足够大,您可能希望通过 const 引用传递它,因此您的 ctor 应该如下所示:

GameContext(World const &world);

First, teminology: You're right that a World * would be a pointer to a World object. A World &, however, would be a reference to a World object. A World would be a copy of the World object, not a reference to it.

The const (used primarily with a pointer or reference, as in World const &world or World const *world) means that you're getting a reference/pointer to a const object -- in other words, you're not allowed to modify the original object to which it refers/points.

For small objects, you usually want to pass a copy. For large objects, you'll typically want to pass a const reference. There are exceptions to this, but that's a reasonable rule of thumb to use until you've learned enough more to know when to break the rules (so to speak).

Just based on the name, I'd guess your World object is probably large enough that you probably want to pass it by const reference, so your ctor should look like:

GameContext(World const &world);
阪姬 2024-09-11 09:56:10

理想情况下,您应该接受对 Worldconst 引用:

class GameContext {
    public:
        GameContext(const World& world);
        virtual ~GameContext();
};

首先,这样在传递 world 时就不会发生复制。其次,const 是关键字,这意味着您不会更改传递的参数,在本例中为 world

Ideally, you should accept a const reference to World:

class GameContext {
    public:
        GameContext(const World& world);
        virtual ~GameContext();
};

First, so that no copying happens when passing the world. Second, const is keyword that means that you aren't going to change the passed parameter which is world in this case.

爱的十字路口 2024-09-11 09:56:10

World 不是对 World 对象的引用 - 编译器正在复制该结构并将其通过堆栈传递。这很糟糕。

指针 (World*) 和引用 (World&) 非常相似,它们仅在语法上有所不同 (->.)和作用域(您无法分配引用,因此如果您使用引用,则成员 m_pWorld 在 GameContext 的生命周期内无法指向另一个世界 - 但您如果您使用指针就可以做到这一点)。我的建议是,如果您总是使用同一个世界(可能),则获取参考,否则使用指针。

其中的 const 告诉编译器您不会修改该对象。当您获得对对象的引用时,您正在处理对象,而不是它的副本 - 因此您在方法中修改的任何内容实际上都会修改“外部”对象。就您而言,由于您可能会在 GameContext 中修改或以其他方式操作您的 World,因此您可能需要一个非常量引用。

所以你应该做这样的事情

class GameContext 
{
public:
    GameContext (World& pWorld) : m_pWorld(pWorld)
    {
    }

    virtual ~GameContext () { ... }

    void foo (int nParam)
    {
        m_pWorld.bar(nParam);
    }

private:
    World& m_pWorld;
};

World is not a reference to a World object - the compiler is making a copy of the structure and passing it through the stack. This is bad.

A pointer (World*) and a reference (World&) are very similar, they differ only in syntax (-> vs .) and scope (you can't assign a reference, so the member m_pWorld can't point to another World during the lifetime of your GameContext if you use a reference - but you can do that if you use a pointer). My advice is get a reference if you'll always use the same World (likely), or a pointer otherwise.

The const there tells the compiler you won't modify the object. When you get a reference to an object, you're working on the object, not a copy of it - so anything you modify in the method actually modifies the "outside" object. In your case, since you'll probably modify or otherwise operate on your World within GameContext, you probably want a non-const reference.

So you should do something like this

class GameContext 
{
public:
    GameContext (World& pWorld) : m_pWorld(pWorld)
    {
    }

    virtual ~GameContext () { ... }

    void foo (int nParam)
    {
        m_pWorld.bar(nParam);
    }

private:
    World& m_pWorld;
};
宛菡 2024-09-11 09:56:10

你的术语太离谱了。

在 C++ 术语中,术语指针地址是同义词(就我们所讨论的值而言)。为了获取指针(或地址),您必须将参数声明为 World* world

要获取引用,参数应声明为 World&世界。

要获取复制值,参数应声明为 World world

在每种具体情况下应使用哪种声明取决于我们的意图。假设 World 是一个重要的对象,选择通常应限于参考 World& 和 World 之一。 world 或指针 World* world

如果您不打算修改 GameContext 构造函数中的源 World 对象,则应添加 constconst World& ; worldconst World* world

最后,通过指针传递允许您传递“保留”值(空指针),但同时会阻止您将指针传递给非左值。如果这对您来说并不重要,那么您可能应该坚持使用参考 const World& 。世界。

Your terminology is way off.

In C++ terminology the terms pointer and address are synonyms (as far as we are talking about values). In order to take the pointer (or the address) you have to declare your parameter as World* world.

To take the reference, the parameter should be declared as World& world.

To take a copied value, the parameter should be declared as World world.

Which declaration you should use in each specific case depends on our intent. Assuming that World is a non-trivial object, the choice should normally be limited to either reference World& world or pointer World* world.

If you don't intend to modify the source World object in GameContext's constructor, a const should be added: const World& world or const World* world.

Finally, passing by pointer allows you to pass a "reserved" value - a null pointer - but at the same time prevents you from passing a pointer to a non-lvalue. If this does not matter much to you, then you should probably stick with a reference const World& world.

流心雨 2024-09-11 09:56:10

引用比指针更安全并且不涉及复制。至于 const 关键字,请阅读我不久前提出的这个问题的答案: C++ 中“const”有多少种用途?

Reference are safer than pointers and involve no copy. As for the const keyword read the answer to this question I made some time ago: How many and which are the uses of "const" in C++?

温馨耳语 2024-09-11 09:56:10

const 意味着方法 GameContext 将无法修改参数 world 的内容

The const means that the method GameContext wont be able to modify the contents of the parameter world

晒暮凉 2024-09-11 09:56:10

GameContext 构造函数应该
获取一个指向 World 对象的指针
(World*),世界地址
对象 (&World) 或对某个对象的引用
世界对象(World)?

首先,“World 对象的地址”与指向 World 对象的指针是一样的。指针是一种存储对象地址的变量。如果你传递一个地址,那么与你传递的地址对应的参数将是一个指针。

是否要获取指针取决于 World 类的语义。如果您采用一个值(而不是指针),那么您将收到传入的 World 对象的副本。如果 GameContext 对象之外的其他对象需要执行操作在那个同一个世界对象上,那么您将需要传入并存储一个指针。如果这不是必需的,或者如果 World 类在内部运行,使得 World 对象的所有副本都使用一组公共数据,那么您可以按值传递。

const关键字什么时候使用
靠近参数?例如:
GameContext(const World &world)

const 表示“无法修改”,就像它在其他地方的含义一样。正如您在示例中给出的,C++ 中的一个常见习惯是采用常量引用而不是采用值。当参数是一个对象(而不是像 intbool 这样的基本类型)时,这比获取值更有效,因为在此不会进行复制案件。然而,const 阻止函数修改引用的参数,因此对于调用者来说,它是一个引用并不重要。

Should the GameContext constructur
take a pointer to a World object
(World*), the address to a World
object (&World) or a reference to a
World object (World)?

First of all, the "address to a World object" is the same thing as a pointer to a World object. A pointer is a type of variable that stores an object's address. If you pass an address, then the parameter that corresponds to the address you passed will be a pointer.

Whether you want to take a pointer or not depends on the semantics of the World class. If you take a value (and not a pointer), then you will receive a copy of the World object passed in. If something other than the GameContext object needs to perform operations on that same world object, then you will want to pass in and store a pointer. If this is not necessary, or if the World class operates internally such that all copies of a World object use a common set of data, then you can pass by value.

What is the const keyword when used
near a parameter? For example:
GameContext(const World &world)

const means "cannot be modified", just like it means anywhere else. A common idom in C++, as you have given in your example, is to take a constant reference instead of taking a value. When the parameter is an object (as opposed to a fundamental type like int or bool), this is more efficient than taking a value, since a copy is not made in this case. However the const prevents the function from modifying the referenced parameter, so to the caller, it does not matter than it is a reference.

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