自動指針

 

 

 

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章