Define a counter class separately to separate the count from the pointer to be monitored for reuse.
Pointer object class implementation
# Include <iostream> using namespace std; /************************ pointer Point class *********** * **************/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 destructor called" <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 ;}
Count
/************************* The counting class that records the number of pointer usage ******* * ******************/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) {// point to the same object if (this = & tUseCount) {return * this;} // Add reference count + + (* tUseCount. m_iUse); // reduce the reference count on the left -- this-> m_iUse; if (0 = this-> m_iUse) {delete this-> m_iUse ;} // point to the same copy this-> m_iUse = TUseCount. m_iUse; return * this;} UseCount ::~ UseCount () {-- (* this-> m_iUse); if (0 = this-> m_iUse) {delete this-> m_iUse ;}} // determine whether the counting object is obtained again, that is, the previous count is already 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 ;}
Smart pointer implementation class
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 )){}; // The preceding constructor or copy constructor can use the default constructor of UseCount to set the corresponding count of UseCount to 1 SmartPoint (SmartPoint & tSmartPoint ); smartPoint & operator = (SmartPoint & tSmartPoint); void SetX (int x); void SetY (int y); int GetX (); int GetY (); void GetUseNum ();~ SmartPoint (); private: // pointer object Point * m_Point; // record the number of times the pointer is used (note that this is an object, not a pointer) 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 ;} // The previous reference count is already 0if (true = this-> m_UseCount.ReAttach (tSmartPoint. m_UseCount) {delete thi S-> 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 () {// when the last object is left, delete the object because the Destructor called by the last object is, then m_UseCount's destructor // will be called to release the counting pointer if (1 = m_UseCount.GetUseNum () {delete m_Point ;}}
Test code
# Include "CSmartPoint. h "# pragma argsusedint 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 <"enter any key to exit ..... "<endl; char c; c = getchar (); return 0 ;}
Test Results
650) this. width = 650; "title =" qq 31002202355.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/19304463D-0.jpg "alt =" 202440917.jpg"/>
This article is from the "Feng qing yang song" blog, please be sure to keep this source http://2309998.blog.51cto.com/2299998/1304175