C++构造函数参数问题
我正在学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
首先,术语:
World *
是指向 World 对象的指针,这是正确的。然而,World &
将是对 World 对象的引用。World
是 World 对象的副本,而不是对其的引用。const
(主要与指针或引用一起使用,如World const &world
或World const *world
)意味着您获取对 const 对象的引用/指针——换句话说,您不允许修改它引用/指向的原始对象。对于小对象,您通常希望传递一个副本。对于大型对象,您通常需要传递 const 引用。虽然也有例外,但这是一个合理的经验法则,直到您了解了足够多的知识以知道何时打破规则(可以这么说)。
仅根据名称,我猜测您的 World 对象可能足够大,您可能希望通过 const 引用传递它,因此您的 ctor 应该如下所示:
First, teminology: You're right that a
World *
would be a pointer to a World object. AWorld &
, however, would be a reference to a World object. AWorld
would be a copy of the World object, not a reference to it.The
const
(used primarily with a pointer or reference, as inWorld const &world
orWorld 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:
理想情况下,您应该接受对
World
的const
引用:首先,这样在传递
world
时就不会发生复制。其次,const
是关键字,这意味着您不会更改传递的参数,在本例中为world
。Ideally, you should accept a
const
reference toWorld
: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 isworld
in this case.World
不是对World
对象的引用 - 编译器正在复制该结构并将其通过堆栈传递。这很糟糕。指针 (
World*
) 和引用 (World&
) 非常相似,它们仅在语法上有所不同 (->
与.
)和作用域(您无法分配引用,因此如果您使用引用,则成员m_pWorld
在 GameContext 的生命周期内无法指向另一个世界 - 但您如果您使用指针就可以做到这一点)。我的建议是,如果您总是使用同一个世界(可能),则获取参考,否则使用指针。其中的
const
告诉编译器您不会修改该对象。当您获得对对象的引用时,您正在处理该对象,而不是它的副本 - 因此您在方法中修改的任何内容实际上都会修改“外部”对象。就您而言,由于您可能会在GameContext
中修改或以其他方式操作您的World
,因此您可能需要一个非常量引用。所以你应该做这样的事情
World
is not a reference to aWorld
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 memberm_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 yourWorld
withinGameContext
, you probably want a non-const reference.So you should do something like this
你的术语太离谱了。
在 C++ 术语中,术语指针和地址是同义词(就我们所讨论的值而言)。为了获取指针(或地址),您必须将参数声明为
World* world
。要获取引用,参数应声明为
World&世界。
要获取复制值,参数应声明为
World world
。在每种具体情况下应使用哪种声明取决于我们的意图。假设
World
是一个重要的对象,选择通常应限于参考World& 和
或指针World
之一。 worldWorld* world
。如果您不打算修改
GameContext
构造函数中的源World
对象,则应添加const
:const World& ; world
或const 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 referenceWorld& world
or pointerWorld* world
.If you don't intend to modify the source
World
object inGameContext
's constructor, aconst
should be added:const World& world
orconst 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
.引用比指针更安全并且不涉及复制。至于 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++?
const
意味着方法GameContext
将无法修改参数world
的内容The
const
means that the methodGameContext
wont be able to modify the contents of the parameterworld
首先,“World 对象的地址”与指向 World 对象的指针是一样的。指针是一种存储对象地址的变量。如果你传递一个地址,那么与你传递的地址对应的参数将是一个指针。
是否要获取指针取决于
World
类的语义。如果您采用一个值(而不是指针),那么您将收到传入的 World 对象的副本。如果GameContext
对象之外的其他对象需要执行操作在那个同一个世界对象上,那么您将需要传入并存储一个指针。如果这不是必需的,或者如果World
类在内部运行,使得World
对象的所有副本都使用一组公共数据,那么您可以按值传递。const
表示“无法修改”,就像它在其他地方的含义一样。正如您在示例中给出的,C++ 中的一个常见习惯是采用常量引用而不是采用值。当参数是一个对象(而不是像int
或bool
这样的基本类型)时,这比获取值更有效,因为在此不会进行复制案件。然而,const 阻止函数修改引用的参数,因此对于调用者来说,它是一个引用并不重要。First of all, the "address to a
World
object" is the same thing as a pointer to aWorld
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 theGameContext
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 theWorld
class operates internally such that all copies of aWorld
object use a common set of data, then you can pass by value.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 likeint
orbool
), this is more efficient than taking a value, since a copy is not made in this case. However theconst
prevents the function from modifying the referenced parameter, so to the caller, it does not matter than it is a reference.