指针与构造函数中的引用 [英] pointer versus reference in constructors

查看:74
本文介绍了指针与构造函数中的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从来都不知道是否应该使用引用或指针来构建器。


任何人都可以给我一些关于何时使用什么的指导?


我的背景是C,所以我倾向于使用指针符号,但实际上并不是
知道参考它的优点是什么。


非常感谢。


ex。两个施工人员


汽车(发动机*发动机)

或者,


汽车(发动机和发动机) ;引擎)

I never really know if I should use references or pointers for constructors.

Can anyone give me some guidance on when to use what?

My back ground is C, so I tend to use pointer notation, but really don''t
know what the advantage of the reference it.

Thanks alot.

ex. of two constructors

Car( Engine* engine )

or,

Car( Engine& engine)

推荐答案

johny smith发布:
johny smith posted:
我真的不知道我是否应该使用引用或
构造函数的指针。

任何人都可以给我一些关于何时使用什么的指导?

我的背景是C,所以我倾向于使用指针表示法,但是真的不知道参考它的优点是什么。

非常感谢。

前。两个施工人员

汽车(发动机*发动机)

或者,

汽车(发动机和发动机)
I never really know if I should use references or pointers for
constructors.

Can anyone give me some guidance on when to use what?

My back ground is C, so I tend to use pointer notation, but really
don''t know what the advantage of the reference it.

Thanks alot.

ex. of two constructors

Car( Engine* engine )

or,

Car( Engine& engine)



级轿车

{

发动机*发动机;

}

如果你有以上,那么你想要存储引擎的地址

对象,所以你需要在构造函数中使用引擎*。

class Car

{

引擎引擎;

}

如果您有上述内容,那么您希望复制一个实际的发动机提供

来自其他地方。在这里你可以使用Engine或Engine&。发动机和放大器;防止创建

临时对象,因此请使用它。虽然你可以使用

一个引擎*来达到这个目的,但引擎和引擎只需保存你输入

星号。

-JKop


class Car
{
Engine* engine;
}
If you have the above, then you''re looking to store the address of an engine
object, so you''ll want to take a Engine* in the contructor.
class Car
{
Engine engine;
}
If you have the above, then you''re looking to copy an actual engine supplied
from elsewhere. Here you may use Engine or Engine&. Engine& prevents a
temporary object being created, so use it. Although ofcourse you could use
an Engine* for this purpose aswell, but the Engine& just saves you typing
the asterisk.
-JKop


一个指针可以用0初始化。 />

参考不能用0初始化。


你的汽车在没有发动机的情况下建造是否合理?如果是这样,请使用

指针。如果没有,请使用参考。


如果您的设计需要使用引擎构建汽车,那么使用

引用有助于在编译时强制执行该约束时间。使用指针

会将该约束的执行移动到运行时。


注意:如Scott Meyers在其Effective C ++中所示;系列可以

有一个空引用...

T * ptr = 0;

T& ref = * ptr;


....但是他将这种用法称为邪恶,并指出了这样的代码的作用

这样的代码的作者应该得到什么命运降临了他们。


他的一个有效的C ++书中包含了关于指针和参考之间的差异的明确条目以及如何确定每种方法的适用时间。
A pointer can be initialized with 0.

A reference may not be initialized with 0.

Is it reasonable that your car be constructed without an engine? If so, use a
pointer. If not, use a reference.

If your design requires a car be constructed with an engine then using a
reference helps to enforce that constraint at compile-time. Using a pointer
would move enforcement of that constraint to run-time.

Note: As shown by Scott Meyers in his "Effective C++" series it is possible to
have a null reference...

T *ptr = 0;
T &ref = *ptr;

....but he refers to such usage as evil and states something to the effect that
the author of such code deserves whatever fate befalls them.

One of his "Effective C++" books contains a nice entry on the differences
between pointers and references and how to determine when each is appropriate.


DaKoadMunky发布:
DaKoadMunky posted:
注意:如Scott Meyers在他的Effective C ++中所示。系列它可能有一个空参考...

T * ptr = 0;
T& ref = * ptr;

......但是他将这种用法称为邪恶,并指出了这些代码的作者应该得到的任何命运降临的效果。
Note: As shown by Scott Meyers in his "Effective C++" series it is
possible to have a null reference...

T *ptr = 0;
T &ref = *ptr;

...but he refers to such usage as evil and states something to the
effect that the author of such code deserves whatever fate befalls
them.



Even如果他们赢得了终身供应的冰淇淋,草莓糖浆和100s

''''1000s?

-JKop


Even if they win a life-time supply of ice-cream, strawberry syrup and 100s
''n'' 1000s?
-JKop


这篇关于指针与构造函数中的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆