自动指针

来源:互联网 发布:eviews软件下载 编辑:程序博客网 时间:2024/05/20 00:37

 

 

 

memory.h#include <memory> ,memorySTD

SYMBIAN

COPY

auto_ptrauto_ptrC++auto_ptr

 

 

——

auto_ptrbug

1.
auto_ptrC++ auto_ptrFord Escort(elmar)

auto_ptrauto_ptr
// 1(a):
//
void f()
{
T* pt( new T );

/*......*/

delete pt;
}

f()f()delete

1(a)

// 1(b): , auto_ptr
//
void f()
{
auto_ptr<T> pt( new T );

/*......*/

} // : pt
//

Tpt

auto_ptrrelease():

// 2: auto_ptr
//
void g()
{
T* pt1 = new T;
//

// auto_ptr
auto_ptr<T> pt2( pt1 );

// auto_ptr
*pt2 = 12; // "*pt1 = 12;"
pt2->SomeFunc(); // "pt1->SomeFunc();"

// get()
assert( pt1 == pt2.get() );

// release()
T* pt3 = pt2.release();

//
// auto_ptr
delete pt3;

} // pt2
// ...ok

auto_ptrreset()auto_ptrauto_ptrreset()auto_ptr

// 3: reset()
//
void h()
{
auto_ptr<T> pt( new T(1) );

pt.reset( new T(2) );
// "new T(1)"T

} // pt
// T

 

原创粉丝点击