关于auto_ptr< class T>的安全性 [英] about the safety of auto_ptr<class T>

查看:93
本文介绍了关于auto_ptr< class T>的安全性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




请参阅此代码:


auto_ptr< int> int_auto_p(new int(3));

auto_ptr< int> int_auto_p2(int_auto_p.get());


然后,两个auto_pointer将拥有相同的对象。这将违反对auto_pointer的

单一所有权期望。


另外,如果我使用它:


int * p = int_auot_p.get();

删除p;


然后,auto_pointer拥有的对象将从外部删除。

这也不安全。


所以,auto_pointer不太安全。


有什么评论吗?

Hi,

see this code:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int_auto_p.get());

Then, both auto_pointer will own the same object. That will violate the
single ownership expectation on auto_pointer.

Also, if i use this:

int * p = int_auot_p.get();
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It''s also not safe.

So, auto_pointer is not so safe.

any comments?

推荐答案



< gu ****** @ gmail.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...

<gu******@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...


看到这段代码:

auto_ptr< int> int_auto_p(new int(3));
auto_ptr< int> int_auto_p2(int_auto_p.get());

然后,两个auto_pointer将拥有相同的对象。这将违反对auto_pointer的单一所有权期望。

另外,如果我使用它:

int * p = int_auot_p.get();
删除p;

然后,auto_pointer拥有的对象将从外部删除。
它也不安全。

所以,auto_pointer不是如此安全。

任何意见?
Hi,

see this code:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int_auto_p.get());

Then, both auto_pointer will own the same object. That will violate the
single ownership expectation on auto_pointer.

Also, if i use this:

int * p = int_auot_p.get();
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It''s also not safe.

So, auto_pointer is not so safe.

any comments?




您期望什么安全?以下不是安全,或者:


int * p1 = new int(3);

int * p2 = p1;

删除p1;

//现在,p2指向已删除的内存。


外部使用内部指针做什么auto_ptr不受

控制auto_ptr。怎么会这样?唯一的选择是

防止使用get(),但这几乎不是一个好的解决方案,会不会是这样的?


基本上,如果你做的事你不应该这样做,你只能责备你的b $ b。不要因为你的拇指而责怪锤子!


-Howard



What safety are you expecting? The following isn''t "safe", either:

int* p1 = new int(3);
int* p2 = p1;
delete p1;
// now, p2 points to deleted memory.

What you do externally with the internal pointer of an auto_ptr is not under
the control of the auto_ptr. How could it be? The only option would be to
prevent the use of get(), but that would hardly be a good solution, would
it?

Basically, if you do things you shouldn''t do, you only have yourself to
blame. Don''t blame the hammer because you hit your thumb!

-Howard


gu ****** @ gmail.com 写道:

所以,auto_pointer不是那么安全。

任何评论?

So, auto_pointer is not so safe.

any comments?




病人:医生,我这样做会很疼。


医生:所以不要这样做。


-


Pete Becker

Dinkumware,Ltd。( http://www.dinkumware.com


gu******@gmail.com 写道:

^^^^

什么是slt代表?
gu******@gmail.com wrote:
^^^^
What''s the "slt" stand for?
参见此代码:

auto_ptr< int> int_auto_p(new int(3));
auto_ptr< int> int_auto_p2(int_auto_p.get());

然后,两个auto_pointer将拥有相同的对象。这将违反对auto_pointer的单一所有权期望。

另外,如果我使用它:

int * p = int_auot_p.get();
删除p;

然后,auto_pointer拥有的对象将从外部删除。
它也不安全。

所以,auto_pointer不是如此安全。

任何评论?
see this code:

auto_ptr<int> int_auto_p(new int(3));
auto_ptr<int> int_auto_p2(int_auto_p.get());

Then, both auto_pointer will own the same object. That will violate the
single ownership expectation on auto_pointer.

Also, if i use this:

int * p = int_auot_p.get();
delete p;

Then, the object owned by auto_pointer will be removed from outside.
It''s also not safe.

So, auto_pointer is not so safe.

any comments?




如果我爬上一把椅子然后首先跳上水泥地板,我会< br $> b $ b最有可能伤害自己,无论是永久性还是致命性。因此,

椅子和水泥地板都不安全。因为他们涉及椅子或地板,地狱,坐着和走路都不安全。您是否知道

过着您的生活导致您的死亡100%确定?


这些是我的评论。


V



If I climb on a chair and then jump on the cement floor head-first, I will
most likely injure myself, either permanently or fatally. So, neither the
chair nor the cement floor are safe. Hell, sitting and walking aren''t
safe then since they involve the chair or the floor. Did you know that
living your life leads to your death with 100% certainty?

These are my comments.

V


这篇关于关于auto_ptr&lt; class T&gt;的安全性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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