关于智能指针的问题 [英] Question about smart pointer
问题描述
大家好:
学会使用智能指针。我有个问题。在下面的
代码:
void foo()
{
auto_ptr< MyClass> p(新MyClass);
p-> DoSomething();
}
函数foo结束时( ),分配给p的内存将自动解除分配。但是如果我有其他函数需要
来访问分配给p的内存,并且最终该内存将由一个函数释放。所以我不希望在函数foo()结束时释放分配给内存的内存。我该怎么办?
非常感谢。
John
Hi all:
Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
Thanks a lot.
John
推荐答案
你能给出模板< class T>的声明/实现吗?上课
auot_ptr {.....}" ;?
2004年6月7日星期一,约翰写道:
can you give the declaration/implementation of "template <class T> class
auot_ptr {.....}"?
On Mon, 7 Jun 2004, John wrote:
大家好:
只是学习使用智能指针。我有个问题。在下面的代码中:
void foo()
{
auto_ptr< MyClass> p(新的MyClass);
p-> DoSomething();
}
在函数foo()的末尾,分配给p的内存将
自动解除分配。但是,如果我有其他功能需要访问分配给p的内存,最终该内存将被一个函数释放。因此我不希望在函数foo()的末尾释放分配给p的内存。我该怎么办?
非常感谢。
约翰
Hi all:
Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
Thanks a lot.
John
- xiaobin
-- xiaobin
" John" <乔********* @ yahoo.com>在留言中写道
news:c3 ************************** @ posting.google.c om ...
"John" <jo*********@yahoo.com> wrote in message
news:c3**************************@posting.google.c om...
大家好:
只是学习使用智能指针。我有个问题。在下面的代码中:
[见下文]
在函数foo()结束时,分配给p的内存将
会自动解除分配。但是,如果我有其他功能需要访问分配给p的内存,最终该内存将被一个函数释放。因此我不希望在函数foo()的末尾释放分配给p的内存。我该怎么办?
void foo()
auto_ptr< MyClass> foo()
{
auto_ptr< MyClass> p(new MyClass);
p-> DoSomething();
返回p;
}
Hi all:
Just learn to use smart pointer. I get a question. In the following
code:
[see below]
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do? void foo()
auto_ptr<MyClass> foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
return p;
}
John写道:
大家好:
只是学习使用智能指针。我有个问题。在下面的代码中:
void foo()
{
auto_ptr< MyClass> p(新的MyClass);
p-> DoSomething();
}
在函数foo()的末尾,分配给p的内存将
自动解除分配。但是,如果我有其他功能需要访问分配给p的内存,最终该内存将被一个函数释放。因此我不希望在函数foo()的末尾释放分配给p的内存。我该怎么办?
非常感谢。
John
Hi all:
Just learn to use smart pointer. I get a question. In the following
code:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
p->DoSomething();
}
By the end of function foo(), the memory that is allocated to p will
be automatically deallocated. But if I have other functions that need
to access the memory allocated to p, and eventually that memory will
be deallocated by one function. So I do not want the memory allocated
to p to be deallocated by the end of function foo(). What should I do?
Thanks a lot.
John
release()方法会导致auto_ptr放弃
内存的所有权。例如:
void foo()
{
auto_ptr< MyClass> p(new MyClass);
MyClass * t;
p-> DoSomething();
//其他东西。
t = p.release();
//现在我们必须手动删除内存。
删除t;
}
艾伦
The release() method will cause the auto_ptr to give up ownership of the
memory. For example:
void foo()
{
auto_ptr<MyClass> p(new MyClass);
MyClass *t;
p->DoSomething();
// Other stuff.
t = p.release();
// Now we must manually delete the memory.
delete t;
}
Alan
这篇关于关于智能指针的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!