我看不出需要auto_ptr?! [英] I cannot see the need for auto_ptr?!

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

问题描述

你好,


我意识到我不知道这个,但我看不出需要auto_ptr。

据我所知,这意味着如果你使用

auto_ptr而不是原始指针创建一个对象,那么当方法/函数结束时,对象将被清除。如果您只是在不使用指针的情况下声明一个

对象,那么这不会发生什么?不是这些例子都是一样的(我知道他们不会,但我希望有人可以向我解释我在这里错过了什么
!):


void f()

{

T t;

t.SomeFunc();

} //功能结束时的清理


void f()

{

auto_ptr< T> ; pt(新T);

pt-> SomeFunc();

} //功能结束时清理


希望我可以看到这里发生了什么!


杰米。

解决方案

Jamie Burns写道:

你好,

我意识到我不知道这个,但我看不出需要auto_ptr。
据我所知,它意味着如果使用
auto_ptr而不是原始指针创建对象,那么当方法/函数结束时,对象将被清除。如果您只是在不使用指针的情况下声明一个
对象,那么这不会发生什么?这些例子不一样(我知道他们不会,但我希望有人可以向我解释我在这里失踪了什么!):

void f()
{t /; t.SomeFunc();
} //功能结束时的清理

void f()
{
auto_ptr< T> pt(新T);
pt-> SomeFunc();
} //功能结束时的清理

希望我能看到这里发生了什么!



所需要的是当对象T不再使用
并且它占用的内存返回到
$时被破坏b $ b系统。使用基于堆栈的对象(你的第一个例子)

当对象离开

范围时会自动发生,但是基于堆的对象(你的第二个例子)

当裸​​指针超出范围时,这不会发生。

auto_ptr是一个基于堆栈的对象被自动销毁

,并在此过程中破坏基于堆的

对象T是auto_ptr包含的。


好的,所以也许我不明白你为什么不会只是在堆栈上分配T?

这是件坏事吗?为什么要在堆上分配如果它需要这种注意清理?


杰米。

" lilburne" <李****** @ godzilla.net>在消息中写道

news:bm ************ @ ID-203936.news.uni-berlin.de ...

Jamie伯恩斯写道:

你好,

我意识到我不知道这个,但我看不出需要
auto_ptr。据我所知,这意味着如果你使用
auto_ptr而不是原始指针创建一个对象,那么当方法/函数结束时,对象将被清理
。如果您只是在不使用指针的情况下声明一个
对象,那么这不会发生什么?不是这些例子一样(我知道他们不会,但我希望有人可以向我解释我在这里缺少的是什么!):

无效f()
{t /; t.SomeFunc();
} //功能结束时的清理

void f()
{
auto_ptr< T> pt(新T);
pt-> SomeFunc();
} //功能结束时的清理

希望我能看到这里发生了什么!


当不再使用对象T并将其占用的内存返回给系统时,需要对象T进行破坏。使用基于堆栈的对象(您的第一个示例)
这会在对象超出范围时自动发生,但是使用基于堆的对象(您的第二个示例)
这不会发生在裸指针超出范围。
auto_ptr是一个基于堆栈的对象被自动销毁,并在此过程中销毁auto_ptr包含的基于堆的对象T。



On Sun,2003年10月19日22:00:22 +0100,Jamie Burns < se ***** @ email.com>

写道:

好的,所以也许我不明白为什么你不会''只是在堆栈上分配T?


因为你希望它比它在堆栈上的寿命更长。

这是一件坏事吗?如果它需要这种类型的注意清理,为什么要尽量在堆上分配?




因为它经常是正确的做。


-

见到你。


Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn''t this what happens if you just declare an
object without using a pointer at all? Aren''t these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!

Jamie.

解决方案

Jamie Burns wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr.
As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when
the method/function ends. Isn''t this what happens if you just declare an
object without using a pointer at all? Aren''t these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am
missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!



What is required is that the object T is destructed when it
is no longer used and the memory it occupied is returned to
the system. With stack based objects (your first example)
this happens automatically when the object goes out of
scope, but with heap based objects (your second example)
this doesn''t occur when a bare pointer goes out of scope.
auto_ptr being a stack based object gets destroyed
automatically, and in the process destroys the heap based
object T that the auto_ptr contains.


OK, so maybe I don''t get why you wouldn''t just allocate T on the stack? Is
that a bad thing to do? Why go out of your way to allocate on the heap if it
requires this type of attention to cleanup?

Jamie.

"lilburne" <li******@godzilla.net> wrote in message
news:bm************@ID-203936.news.uni-berlin.de...

Jamie Burns wrote:

Hello,

I realise that I just dont get this, but I cannot see the need for auto_ptr. As far as I have read, it means that if you create an object using an
auto_ptr, instead of a raw pointer, then the object will get cleaned up when the method/function ends. Isn''t this what happens if you just declare an
object without using a pointer at all? Aren''t these examples the same (I
know they will not be, but I am hoping someone may explain to me what I am missing here!):

void f()
{
T t;
t.SomeFunc();
} // cleanup when function ends

void f()
{
auto_ptr<T> pt(new T);
pt->SomeFunc();
} // cleanup when function ends

Wish I could see what is going on here!



What is required is that the object T is destructed when it
is no longer used and the memory it occupied is returned to
the system. With stack based objects (your first example)
this happens automatically when the object goes out of
scope, but with heap based objects (your second example)
this doesn''t occur when a bare pointer goes out of scope.
auto_ptr being a stack based object gets destroyed
automatically, and in the process destroys the heap based
object T that the auto_ptr contains.



On Sun, 19 Oct 2003 22:00:22 +0100, "Jamie Burns" <se*****@email.com>
wrote:

OK, so maybe I don''t get why you wouldn''t just allocate T on the stack?
Because you want it to live longer than it would on the stack.
Is that a bad thing to do? Why go out of your way to allocate on the heap if
it requires this type of attention to cleanup?



Because it is very often the right thing to do.

--
Be seeing you.


这篇关于我看不出需要auto_ptr?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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