溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

3種智能指針

發(fā)布時(shí)間:2020-08-03 19:11:34 來源:網(wǎng)絡(luò) 閱讀:387 作者:2013221 欄目:編程語言
  • 出現(xiàn)智能指針的原因

     用智能指針,把申請(qǐng)內(nèi)存的工作都在接口內(nèi)部實(shí)現(xiàn)并加以限制,把釋放內(nèi)存的工作交給智能指針。
  • 常見的智能指針

     1.你知道智能指針嗎?智能指針的原理。
     2.常用的智能指針。
     3.智能指針的實(shí)現(xiàn)。

  1答案:智能指針是一個(gè)類,這個(gè)類的構(gòu)造函數(shù)中傳入一個(gè)普通指針,析構(gòu)函數(shù)中釋放傳入的指針。智能指針的類都是棧上的對(duì)象,所以當(dāng)函數(shù)(或程序)結(jié)束時(shí)會(huì)自動(dòng)被釋放,

     2, 最常用的智能指針: 

      1)std::auto_ptr,有很多問題。 不支持復(fù)制(拷貝構(gòu)造函數(shù))和賦值(operator =),但復(fù)制或賦值的時(shí)候不會(huì)提示出錯(cuò)。因?yàn)椴荒鼙粡?fù)制,所以不能被放入容器中。

     2) C++11引入的unique_ptr(scoped_ptr), 也不支持復(fù)制和賦值,但比auto_ptr好,直接賦值會(huì)編譯出錯(cuò)。實(shí)在想賦值的話,需要使用:std::move。

      例如:

          std::unique_ptr<int> p1(new int(5));
          std::unique_ptr<int> p2 = p1; // 編譯會(huì)出錯(cuò)
          std::unique_ptr<int> p3 = std::move(p1); // 轉(zhuǎn)移所有權(quán), 現(xiàn)在那塊內(nèi)存歸p3所有, p1成為無效的指針.

      3) C++11或boost的shared_ptr,基于引用計(jì)數(shù)的智能指針??呻S意賦值,直到內(nèi)存的引用計(jì)數(shù)為0的時(shí)候這個(gè)內(nèi)存會(huì)被釋放。

      4)C++11或boost的weak_ptr,弱引用。 引用計(jì)數(shù)有一個(gè)問題就是互相引用形成環(huán),這樣兩個(gè)指針指向的內(nèi)存都無法釋放。需要手動(dòng)打破循環(huán)引用或使用weak_ptr。顧名思義,weak_ptr是一個(gè)弱引用,只引用,不計(jì)數(shù)。如果一塊內(nèi)存被shared_ptr和weak_ptr同時(shí)引用,當(dāng)所有shared_ptr析構(gòu)了之后,不管還有沒有weak_ptr引用該內(nèi)存,內(nèi)存也會(huì)被釋放。所以weak_ptr不保證它指向的內(nèi)存一定是有效的,在使用之前需要檢查weak_ptr是否為空指針。

  • 智能指針的實(shí)現(xiàn)

  • Autoptr

    代碼:

#pragma once

template<class T>
class Autoptr
{
public:
	Autoptr()
		:_ptr(NULL)
	{}
	Autoptr(T *ptr)
		:_ptr(ptr)
	{}
	Autoptr(Autoptr<T>& a)
		:_ptr(a._ptr)
	{
		delete _ptr;
		a._ptr = NULL;
	}
	~Autoptr()
	{
		if (_ptr)
		{
			delete _ptr;
			_ptr = NULL;
		}
	}
	Autoptr<T>& operator=(Autoptr<T>&a)
	{
		if (this != &a)
		{
			delete _ptr;
			_ptr = a._ptr;
			a._ptr = NULL;
		}
		return *this;
	}
	T& operator*()
	{
		return *_ptr;
	}
	T* Getptr()
	{
		return _ptr;
	}

protected:
	T *_ptr;
};
void Autoptrtest()
{
	int *a = new int(5);
	Autoptr<int> ap1(a);
	cout << *ap1 << endl;
	cout << ap1.Getptr() << endl;
	Autoptr<int> ap2(ap1);
	cout << *ap2 << endl;
	cout << ap2.Getptr() << endl;
	cout << ap1.Getptr() << endl;
	Autoptr<int> ap3;
	ap3 = ap2;
	cout << *ap3 << endl;
	cout << ap3.Getptr() << endl;
	cout << ap2.Getptr() << endl;
}
  • Scopedptr

   代碼:

#pragma once
template<class T>
class Scopedptr
{
public:
	Scopedptr()
		:_ptr(NULL)
	{}
	Scopedptr(T *data)
		:_ptr(data)
	{}
	~Scopedptr()
	{
		if (_ptr)
		{
			delete _ptr;
			_ptr = NULL;
		}
	}
	T& operator*()
	{
		return *_ptr;
	}
	T* Getptr()
	{
		return _ptr;
	}
protected://加上protected可以防止使用者在類之外定義拷貝構(gòu)造和運(yùn)算符的重載函數(shù) 
	Scopedptr<T>(const Scopedptr<T>&sp); //不讓使用者使用拷貝,可以防止拷貝,所以只聲明不定義
	Scopedptr<T>& operator=(const Scopedptr<T>&sp);
private:
	T *_ptr;
};
void Scopedptrtest()
{
	int *a = new int(5);
	Scopedptr<int> ap1(a);
	cout << *ap1 << endl;
	cout << ap1.Getptr() << endl;
	/*Scopedptr<int> ap2(ap1);
	cout << *ap2 << endl;
	Scopedptr<int> ap3;
	ap3 = ap2;
	cout << *ap3 << endl;
	cout << ap3.Getptr() << endl;
	cout << ap2.Getptr() << endl;*/
}
  • Sharedptr

    代碼:

#pragma once
template<class T>
class Sharedptr
{
public:
	Sharedptr()
		:_ptr(NULL)
		, _pcount(new int(1))
	{}
	Sharedptr(T *ptr)
		:_ptr(ptr)
		, _pcount(new int(1))
	{}
	Sharedptr(const Sharedptr<T>& sp)
		:_ptr(sp._ptr)
		, _pcount(sp._pcount)
	{
		++(*_pcount);
	}
	~Sharedptr()
	{
		if (_ptr)
		{
			if (--(*_pcount)==0)
			{
				delete _ptr;
				delete _pcount;
				_ptr = NULL;
				_pcount = NULL;
			}
			_ptr = NULL;
		}
	}
	Sharedptr<T>& operator=(const Sharedptr<T> &sp)
	{
		if (this != &sp)
		{
			if (--(*_pcount) == 0)
			{
				delete _ptr;
				delete _pcount;
				_ptr = NULL;
				_pcount = NULL;
			}
			_ptr = sp._ptr;
			_pcount = sp._pcount;
			++(*_pcount);
		}
		return *this;
	}
private:
	T* _ptr;
	int *_pcount;
};
void Sharedptrtest()
{
	int *a = new int(5);
	Sharedptr<int> ap1(a);
	Sharedptr<int> ap2(ap1);
	Sharedptr<int> ap3;
	ap3 = ap2;
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI