智能指針

       智能指針是可以智能化的動態開闢空間和內存釋放。C++中引入智能指針,是防止咱們在動態開闢空間是,因爲疏忽大意,致使內存的釋放出現問題,最終導致內存泄漏。ios

智能指針的基本特色:ide

(1)智能指針管理的是一塊內存的釋放。函數

(2)智能指針是一個類,有相似指針的功能。this

AutoPtr:spa


#include<iostream>指針

using namespace std;內存

template<typename T>it

class AutoPtrio

{class

public:

AutoPtr(T* ptr=NULL)

:_ptr(ptr)

{}

AutoPtr(AutoPtr<T> & ap)

:_ptr(ap._ptr)

{

ap._ptr = NULL;

}

AutoPtr<T>&operator=(AutoPtr<T>& ap)

{

if (this != &ap)

{

if (_ptr != NULL)

{

delete _ptr;

}

_ptr = ap._ptr;

ap._ptr = NULL;

}

return *this;

}

T& operator* ()

{

return *_ptr;

}

T* operator->()

{

return _ptr;

}

~AutoPtr()

{

if (NULL != _ptr)

{

delete _ptr;

}

}

protected:

T * _ptr;

};


struct Point

{

int x;

int y;

};

void FunTest()

{

int *p = new int[10];

delete [] p;

AutoPtr<int> ap = new int(10);

AutoPtr<int> ap1(ap);

AutoPtr<int> ap2;

ap2 = ap1;

*ap2 = 10;

AutoPtr<Point> ap3(new Point);

(*ap3).x = 10;

ap3->y = 10;

}

int main()

{

FunTest();

return 0;

}


ScopedPtr:由於智能指針容易出現拷貝時釋放兩次的狀況,因此ScopedPtr主要是進行防止拷貝防止拷貝的兩條必需要知足的條件是:(1)設置保護限定符(2)對拷貝構造函數和賦值運算符重載進行只聲明不定義。

 如若只有(2),沒有設置保護限定符,若在類外進行定義後,則會出現問題,因此說這兩個條件是必不可少的。這樣就可以避免上面所出現的問題,可是這樣就形成了它在功能上的缺陷。


#include<iostream>

using namespace std;


template<typename T>

class ScopedPtr

{

friend void FunTest();

public:

ScopedPtr(T * ptr = NULL)

:_ptr(ptr)

{}

~ScopedPtr()

{

if (NULL != _ptr)

{

delete _ptr;

_ptr = NULL;

}

}

private:

ScopedPtr (ScopedPtr<T>& ap);

ScopedPtr<T>& operator =(ScopedPtr<T>&ap);

private:

T * _ptr;

};


void FunTest()

{

ScopedPtr<int>ap(new int(10));

//ScopedPtr<int>ap1(ap);

}

int main()

{

FunTest();

return 0;

}


SharedPtr:SharedPtr指針主要的原理是利用引用計數的淺拷貝來實現,經過多開闢4個字節的方式,存儲引用計數,當有指針指向這塊空間時,引用計數+1。如若析構時,先將這塊空間的引用計數降爲1,而後在進行析構,避免了析構屢次的問題


#include<iostream>

using namespace std;


template<typename T>

class SharedPtr

{

public:

SharedPtr(T* ptr = NULL)

:_ptr(ptr)

, _pCount(new int (1))

{}

SharedPtr(SharedPtr<T>& ap)

:_ptr(ap._ptr)

, _pCount(ap._pCount)

{

(*_pCount)++;

}

SharedPtr<T>& operator=(const SharedPtr<T>& ap)

{

if (this != &ap)

{

if (--(*_pCount) == 0)

{

delete _ptr;

delete _pCount;

}

_ptr = ap._ptr;

_pCount = ap._pCount;

++(*_pCount);

}

return *this;

}

~SharedPtr()

{

if (--(*_pCount) == 0)

{

delete _ptr;

delete _pCount;

}

}

private:

T * _ptr;

int * _pCount;

};

int main()

{

int *p = new int;

int *q = p;

SharedPtr<int> ap1(new int(10));

SharedPtr<int> ap2(ap1);

SharedPtr<int> ap3;

ap3 = ap2;

SharedPtr<int> ap4(new int(20));

return 0;

}

相關文章
相關標籤/搜索