指针问题 [英] Pointer question

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

问题描述




如果我有这样的话:

CMyClass * p1;

CMyClass * p2;


我知道我能做到:

p1 =新CMyClass();

p2 = p1;

现在我的两个指针都指向同一个对象。到目前为止很酷。


但是有可能这样做 - 在你初始化你的指针之前吗?


p2 = p1;

p1 =新的CMyClass();


并且两个指针都指向同一个对象?


我不喜欢我想是的,但我想我肯定会检查一下。


谢谢,

布莱恩

Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?

I don''t think so, but thought I''d check for sure.

Thanks,
Bryan

推荐答案

BCC< a@b.c>在消息中写道

新闻:wI ********************* @newssvr21.news.prodig y.com ...
BCC <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig y.com...


如果我有这样的东西:
CMyClass * p1;
CMyClass * p2;

我知道我可以do:
p1 = new CMyClass();
p2 = p1;

现在我的两个指针指向同一个对象。到目前为止很酷。

但是有可能这样做 - 在你初始化你的指针之前吗?

p2 = p1;
p1 = new CMyClass();

并且两个指针都指向同一个对象?
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?

p2 = p1;
p1 = new CMyClass();

and have both pointers point to the same object?




除非您的计算机使用tachyons,否则没有。


如果p1还没有在第一行之前初始化,那么就有一个极其遥远的可能性,它会意外地成为同一个地址

由新返回表达。


DW



Unless your computer runs on tachyons, then no.

If p1 has not been initialized before the first line, then there is an
extremely remote possibility that it will accidentally be the same address
as that returned by the "new" expression.

DW




" BCC" < a@b.c>在消息中写道

新闻:wI ********************* @newssvr21.news.prodig y.com ...

"BCC" <a@b.c> wrote in message
news:wI*********************@newssvr21.news.prodig y.com...


如果我有这样的东西:
CMyClass * p1;
CMyClass * p2;

我知道我可以do:
p1 = new CMyClass();


不需要大括号。

p2 = p1;

现在我的两个指针都指向同一个对象。到目前为止很酷。

但是有可能这样做 - 在你初始化你的指针之前吗?


No.
p2 = p1;


复制垃圾邮件地址p1持有p2。

p1 = new CMyClass();


p1现在持有一个有效的堆地址,而p2仍然保存垃圾地址。
并且两个指针指向同一个对象?
Hi,

If I have something like this:
CMyClass* p1;
CMyClass* p2;

I know I can do:
p1 = new CMyClass();
No need of the braces.
p2 = p1;

Now both my pointers point to the same object. Cool so far.

But is it possible to do this -before- you initialize your pointers?
No.
p2 = p1;
Copy the junk address p1 is holding into p2.
p1 = new CMyClass();
p1 now holds a valid heap address while p2 still holds the junk address.
and have both pointers point to the same object?




你现在看到差异了吗?


-

祝你好运,

J.Schafer



You see the difference now?

--
With best wishes,
J.Schafer


David White< no@email.provided>在消息中写道

news:w5 ***************** @ nasal.pacific.net.au ...
David White <no@email.provided> wrote in message
news:w5*****************@nasal.pacific.net.au...
BCC< a@b.c>在消息中写道
如果p1在第一行之前尚未初始化,则有一个非常遥远的可能性,它将意外地与new返回的地址相同。表达。
BCC <a@b.c> wrote in message
If p1 has not been initialized before the first line, then there is an
extremely remote possibility that it will accidentally be the same address
as that returned by the "new" expression.




或者这可能会在某些编译器上执行:

p1 = new CMyClass;

delete p1 ;

//现在你的代码

p2 = p1;

p1 =新的CMyClass();


DW



Or this might do it on some compilers:
p1 = new CMyClass;
delete p1;
// now your code
p2 = p1;
p1 = new CMyClass();

DW


这篇关于指针问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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