何时在类中使用指针? [英] When to use pointers in a class?

查看:64
本文介绍了何时在类中使用指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一些经验法则关于何时使用指向对象的指针和

何时在类需要有对象时对对象使用引用*

作为数据成员?


示例:

A级

{

B * b_ptr;

B b;

vector< B *> vector_ptrs;

vector< B> vector_objects;

};


如何知道何时使用指针以及何时不使用指针?


谢谢,

Joe


*我不确定我的术语是否正确,请纠正我如果

我错了。我也对

引用和指针之间的关系感到有些困惑。我知道引用是一个对象的别名

和一个指针包含一些地址,通常是

对象在内存中的位置的开始,但是我还不确定真正的

差异是什么。

Is there some rule of thumb about when to use pointers to an object and
when to use a reference* to an object when a class needs to have objects
as data members?

Example:
class A
{
B* b_ptr;
B b;
vector<B*> vector_ptrs;
vector<B> vector_objects;
};

How do I know when to use pointers and when not to use them?

Thanks,
Joe

* I''m not sure if my terminology is correct here, please correct me if
I''m wrong. I''m also slightly confused about the relationship between
references and pointers. I know a reference is an alias of an object
and a pointer contains some address that''s usually the start of the
object''s location in memory, but I''m still not sure what the real
difference is.

推荐答案

Joe Van Dyk写道:
Joe Van Dyk wrote:
当一个类需要有对象作为数据成员时,是否有一些经验法则关于何时使用指向对象的指针以及何时使用对象的引用? ?


除非你需要一个指针的额外功能,否则更喜欢参考。

示例:
A级
{
B * b_ptr;


首选参考(坐在A :: A中),除非你需要b_ptr == NULL用于特殊的

情况。

然后,当你的设计变得更高级时,创建一个NullB,派生自B,

,用于没有B,座位或点b_ptr。 NULL指针

应该替换为什么都不做的Null对象。这简化了

调用代码。

B b;
vector< B *> vector_ptrs;


指针可以作为对象复制,因此你不能在那里使用引用。 (我

怀疑模板甚至会扩展到一个。

vector< B> vector_objects;


这是副本,不是参考。如果B是干净的,可以选择一个,优先选择指针_或者参考。


越简越好。

*我不确定我的术语在这里是否正确,如果我错了,请纠正我。我也对
引用和指针之间的关系感到有些困惑。我知道一个引用是一个对象的别名
而一个指针包含一些地址'通常是
对象'在内存中的位置的开始,但我仍然不确定是什么真正的区别是。
Is there some rule of thumb about when to use pointers to an object and
when to use a reference to an object when a class needs to have objects
as data members?
Prefer a reference unless you need a pointer''s extra features.
Example:
class A
{
B* b_ptr;
Prefer a reference (seated in A::A), unless you need b_ptr==NULL for special
situations.

Then, as your designing gets more advanced, create a NullB, derived from B,
for use when there''s no B, and seat or point b_ptr to it. NULL pointers
should be replaced with Null Objects that do nothing. This simplifies the
calling code.
B b;
vector<B*> vector_ptrs;
A pointer can be copied as an object, so you can''t use a reference there. (I
doubt the template will even expand on one.
vector<B> vector_objects;
That is a copy, not a reference. Prefer one, if B is cleanly copiable, in
preference to pointers _or_ references.

The simpler the better.
* I''m not sure if my terminology is correct here, please correct me if
I''m wrong. I''m also slightly confused about the relationship between
references and pointers. I know a reference is an alias of an object
and a pointer contains some address that''s usually the start of the
object''s location in memory, but I''m still not sure what the real
difference is.




有时引用是指向间接*已经打开的指针。

那个''如何开始考虑它们,但别名定义更强大,因为语言允许微妙的优化,例如

temporaries,通过这个定义。


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



Sometimes a reference is a pointer with the indirection * already turned on.
That''s how to start thinking about them, but the alias definition is more
powerful because the language permits subtle optimizations, such as
temporaries, via that definition.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!




" Phlip" < pH值******* @ gmail.com>在消息中写道

news:35 ************* @ newssvr24.news.prodigy.net ...

"Phlip" <ph*******@gmail.com> wrote in message
news:35*************@newssvr24.news.prodigy.net...
Joe Van Dyk写道:
Joe Van Dyk wrote:
是否有一些关于何时使用指针指向对象的经验法则以及何时在类需要对象时使用对象的引用作为数据成员?
除非您需要指针的额外功能,否则请更喜欢参考。
Is there some rule of thumb about when to use pointers to an object and
when to use a reference to an object when a class needs to have objects
as data members?
Prefer a reference unless you need a pointer''s extra features.




作为数据成员?为什么?我很少使用引用作为成员。


我会说更喜欢使用成员对象(例如

中的成员''b''示例)指针成员(例如示例中的成员''b_ptr')。


也许您正在考虑函数参数?对于那些,我同意

引用(或const引用)可能比指针更可取,除非

需要一个指针,例如允许NULL值。



As a data member? Why? I rarely use references as members.

I''d say prefer to use a member object (such as the member ''b'' in the
example) over pointer members (such as member ''b_ptr'' in the example).

Perhaps you''re thinking of function parameters? For those, I''d agree that a
reference (or const reference) may be preferable to a pointer, unless there
is some need for a pointer, such as allowing a NULL value.

示例:
A类
{B / B_ptr;
喜欢参考(坐在A: :A),除非你需要b_ptr == NULL才能获得特殊的
情况。
Example:
class A
{
B* b_ptr;
Prefer a reference (seated in A::A), unless you need b_ptr==NULL for
special
situations.




我不明白。你的意思是你更喜欢


A级

{

B& b;

// ...

};


over


A级

{

B b;

// ...

};


....?


听起来你假设B的一个实例位于A

类之外,在构造A的实例之前,可以将
传递给A'的构造函数。但OP没有做出这样的推断。

然后,随着你的设计越来越先进,创建一个NullB,派生自
B,
在没有B时使用,座位或点b_ptr到它。 NULL指针
应该替换为什么都不做的Null对象。这简化了呼叫代码。



I don''t understand. You mean you''d prefer

class A
{
B& b;
//...
};

over

class A
{
B b;
//...
};

....?

Sounds like you''re assuming that an instance of B resides outside the A
class, prior to the construction of an instance of A, which is available to
pass to A''s constructor. But the OP made no such inference.

Then, as your designing gets more advanced, create a NullB, derived from
B,
for use when there''s no B, and seat or point b_ptr to it. NULL pointers
should be replaced with Null Objects that do nothing. This simplifies the
calling code.

B b;
vector< B *> vector_ptrs;
B b;
vector<B*> vector_ptrs;



指针可以作为对象复制,所以你不能在那里使用引用。
(我怀疑模板会扩展一个。



A pointer can be copied as an object, so you can''t use a reference there.
(I
doubt the template will even expand on one.

vector< B> vector_objects;
vector<B> vector_objects;



这是一个副本,而不是一个参考。如果B是完全可以复制的,请选择一个,
首选指针_or_ references。



That is a copy, not a reference. Prefer one, if B is cleanly copiable, in
preference to pointers _or_ references.




好​​吧,至少有两个原因我可以(通常在
$中) b $ b practice)更喜欢向量而不是对象的指针。一个是我可能想要

来利用多态,我只能用指针做这个或者

参考。另一个是复制对象_may_是昂贵的或

否则不受欢迎。


智能指针作为矢量成员,我被告知,是一个偶数更好的解决方案

很多情况。


-Howard



Well, there are at least two reasons why I might (and usually do, in
practice) prefer pointers in vectors over objects. One is that I might want
to make use of polymorphism, and I can only do that with a pointer or
reference. The other is that the copying of objects _may_ be expensive or
otherwise undesired.

Smart pointers as vector members, I am told, are an even better solution in
many cases.

-Howard


Howard写道:
Howard wrote:
除非您需要指针的额外功能,否则请更喜欢参考。
作为数据成员?为什么?我很少使用引用作为成员。
Prefer a reference unless you need a pointer''s extra features.
As a data member? Why? I rarely use references as members.




我很少编写新的C ++代码。我的工作是排除现有的C ++故障。学习C风格的代码

,例如指针滥用,让我疯狂。但是,这并不一定会减慢我的速度。


也许你的成员经常会对其他物品进行指责?然后你需要

指针。

也许你在考虑函数参数?对于那些,我同意
引用(或const引用)可能比指针更可取,除非需要指针,例如允许NULL值。


同样的规则;仅当您需要其额外功能时才使用指针。


现在阅读MFC代码或驱动RenderWare的C ++代码。每个人都传递指针

无处不在,即使传递NULL也毫无意义,即使在
NullObject完美运行的情况下也是如此。 Bleah!

我不明白。你的意思是你更喜欢

A级。
B& b;
// ...
};

结束

A级
{B / B} /> // ...
};

......?


如果A可以拥有B,请使用最后一个。它是_weakest_构造,

_least_额外功能。

听起来你假设B的一个实例位于A的外面
class,在构造A的实例之前,可以传递给A'的构造函数。但OP没有做出这样的推断。


我做了。我知道OP知道喜欢成员对象而不是手柄



嗯,我至少有两个原因(并且通常在实践中更喜欢向量而不是对象的指针。一个是我可能想要利用多态,我只能用指针或
引用来做。另一个是复制对象_可能是昂贵的,或者其他不受欢迎的。


当你需要指针提供的额外功能时,请使用它们。

智能指针作为矢量成员,我被告知,是一个更好的解决方案在很多情况下。




如果他们确实足够聪明;-)


-

Phlip
http://www.greencheese.org/ZeekLand < - 不是博客!!!



If they are indeed smart enough ;-)

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


这篇关于何时在类中使用指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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