程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> C++沉思錄之智能指針句柄

C++沉思錄之智能指針句柄

編輯:關於C語言

通過單獨定義一個計數器類,將計數和其所要監視的指針分離出來,方便復用。

指針對象類實現

#include <iostream>
using namespace std;
/*************************
指針Point 類
**************************/
class Point
{
public:
Point():m_x(0),m_y(0){};
Point(int &x,int &y):m_x(x),m_y(y){};
Point(const Point& tPoint);
int GetX();
int GetY();
void SetX(int x);
void SetY(int y);
~Point(){cout<<"Point 析構函數調用了"<<endl;}
private:
int m_x;
int m_y;
};
Point::Point(const Point& tPoint)
{
this->m_x = tPoint.m_x;
this->m_y = tPoint.m_y;
}
int Point::GetX()
{
return this->m_x;
}
int Point::GetY()
{
return this->m_y;
}
void Point::SetX(int x)
{
this->m_x = x;
}
void Point::SetY(int y)
{
this->m_y = y;
}

計數類

/*************************
記錄指針使用次數的計數類
**************************/
class UseCount
{
public:
UseCount():m_iUse(new int(1)){};
UseCount(const UseCount&tUseCount);
UseCount& operator = (UseCount&tUseCount);
~UseCount();
bool ReAttach(UseCount& tUseCount);
int GetUseNum();
private:
int *m_iUse;
};
UseCount::UseCount(const UseCount&tUseCount)
{
this->m_iUse = tUseCount.m_iUse;
++(*this->m_iUse);
}
UseCount& UseCount::operator = (UseCount& tUseCount)
{
//指向同一對象
if(this == &tUseCount)
{
return *this;
}
//增加右側的引用計數
++(*tUseCount.m_iUse);
//減少左側的引用計數
--this->m_iUse;
if(0 == this->m_iUse)
{
delete  this->m_iUse;
}
//指向同一副本
this->m_iUse = tUseCount.m_iUse;
return *this;
}
UseCount::~UseCount()
{
--(*this->m_iUse);
if(0 == this->m_iUse)
{
delete this->m_iUse;
}
}
//判斷是否是重新得到的計數對象,就是之前計數已經為0
bool UseCount::ReAttach(UseCount& tUseCount)
{
++(*tUseCount.m_iUse);
--(*this->m_iUse);
if(0 == *this->m_iUse)
{
delete m_iUse;
m_iUse = tUseCount.m_iUse;
return true;
}
m_iUse = tUseCount.m_iUse;
return false;
}
int UseCount::GetUseNum()
{
return *this->m_iUse;
}

智能指針實現類

class SmartPoint
{
public:
SmartPoint():m_Point(new Point()){};
SmartPoint(int x,int y):m_Point(new Point(x,y)){};
SmartPoint(const Point &tPoint):m_Point(new Point(tPoint)){};
//上面的這些構造函數或拷貝構造函數可以利用UseCount的
//缺省構造函數使UseCount的對應計數為1
SmartPoint( SmartPoint &tSmartPoint);
SmartPoint & operator = (SmartPoint &tSmartPoint);
void SetX(int x);
void SetY(int y);
int GetX();
int GetY();
void GetUseNum();
~SmartPoint();
private:
//指針對象
Point *m_Point;
//記錄指針的使用次數(注意這裡是對象,不是指針)
UseCount m_UseCount;
};
SmartPoint::SmartPoint(SmartPoint &tSmartPoint)
{
this->m_Point = tSmartPoint.m_Point;
this->m_UseCount =  tSmartPoint.m_UseCount;
}
SmartPoint& SmartPoint::operator = (SmartPoint &tSmartPoint)
{
if(this == &tSmartPoint)
{
return *this;
}
//之前的引用計數已經為0
if(true ==  this->m_UseCount.ReAttach(tSmartPoint.m_UseCount))
{
delete this->m_Point;
}
this->m_Point = tSmartPoint.m_Point;
return *this;
}
void SmartPoint::SetX(int x)
{
this->m_Point->SetX(x);
}
void SmartPoint::SetY(int Y)
{
this->m_Point->SetY(Y);
}
int SmartPoint::GetX()
{
return this->m_Point->GetX();
}
int SmartPoint::GetY()
{
return this->m_Point->GetY();
}
void SmartPoint::GetUseNum()
{
cout<<"use number is "<<m_UseCount.GetUseNum()<<endl;
}
SmartPoint::~SmartPoint()
{
//剩下最後一個對象時,刪除該對象,因為是
//最後一個對象調用的析構函數,之後m_UseCount的析構函數
//會被調用釋放計數指針
if(1 == m_UseCount.GetUseNum())
{
delete m_Point;
}
}

測試代碼

#include "CSmartPoint.h"
#pragma argsused
int main(int argc, char* argv[])
{
SmartPoint *PA = new SmartPoint();
SmartPoint *PB = new SmartPoint(*PA);
SmartPoint *PC = new SmartPoint();
*PC =  *PA;
PC->GetUseNum();
PA->GetUseNum();
delete PA;
PC->GetUseNum();
delete PB;
PC->GetUseNum();
delete PC;
cout<<"輸入任意鍵退出....."<<endl;
char c;
c=getchar();
return 0;
}

測試結果

202440917.jpg

本文出自 “風清揚song” 博客,請務必保留此出處http://2309998.blog.51cto.com/2299998/1304175

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved