C ++ smart pointer

Source: Internet
Author: User

C ++ gave users the ability to fully apply for and release, and when the pointer was improperly used, it was easy to hurt their hands. Especially when pointers are used in classes, it is easy to cause memory leakage caused by forgetting to release memory, and pointer errors caused by releasing released memory. Therefore, a pointer reference counting smart pointer is required to manage the usage of pointers.

First, add a management class to the object to be used as a pointer. This class stores the Class Object (note that this is the object of the original class) and the number of times it is used, this class is used to save an object copy of the original class. In the future, both the copy structure and the copy reference will maintain a copy of the pointer. Deep copy is also implemented based on actual needs, and each of them maintains a copy. Currently, we implement the shortest copy function.

The Code is as follows:

Class to be a pointer

/***************** Original vertex class implementation **************** **/class Point {public: point (): m_x (0), m_y (0) {}; Point (int x, int y): m_x (x), m_y (y ){};~ Point () {cout <"Point class is destructed" <endl ;}; void SetX (int x); void SetY (int y); int GetX (); int GetY (); string m_PointName; private: int m_x; int m_y;}; void Point: SetX (int x) {this-> m_x = x;} void Point :: setY (int y) {this-> m_y = y;} int Point: GetX () {return this-> m_x;} int Point: GetY () {return this-> m_y ;}

Class that records pointer objects

// Forward Declaration, indicating that the class SmartPoint must be defined later; /*********************************** pointer management class, this implementation aims to record the number of objects with pointers using ***************************** * ******/class UPoint {// all the Members and variables of this class are private, in this way, the smart pointer class can only be private by smart pointers: // here, the smart pointer class is defined as a friend. You can access this class's private member friend class SmartPoint; UPoint (): m_iUse (1) {}; UPoint (int x, int y): m_Point (x, y), m_iUse (1) {}; UPoint (const Point & tPoint): m_Point (tPoint ), m_iUse (1) {}; // note that the object is saved here, instead of a pointer, It is the pointer Point m_Point; int m_iUse;} to record the usage ;};

Object Pointer implementation smart pointer

/******************************* Intelligent pointer implementation class **** * ***************************/class SmartPoint {public: smartPoint (): m_UPoint (new UPoint () {}; SmartPoint (int x, int y): m_UPoint (new UPoint (x, y )){}; smartPoint (const Point & tPoint): m_UPoint (new UPoint (tPoint) {}; // copy construction and replication operator SmartPoint (const SmartPoint & tSmartPoint ); smartPoint & operator = (const SmartPoint & tSmartPoint );~ SmartPoint (); void SetX (int x); void SetY (int y); int GetX (); int GetY (); void SetName (string sName); void ShowInfo (); private: // note that a pointer is defined here. All Smart pointers direct to the same object // This pointer UPoint * m_UPoint;} is released only when the reference count is 0 ;}; // ----------------------------------------------------------------------------- SmartPoint ::~ SmartPoint () {-- this-> m_UPoint-> m_iUse; cout <"remaining number of calls to the smart pointer destructor" <this-> m_UPoint-> m_iUse <endl; if (0 = this-> m_UPoint-> m_iUse) {cout <"pointer monitored by smart pointer is released" <endl; delete this-> m_UPoint ;}} /*************************************** ** copy constructor *********************************** * *****/SmartPoint:: SmartPoint (const SmartPoint & tSmartPoint) {// pointer shortest copy, pointing to the same copy this-> m_UPoint = tSmartPoint. m_UPoint; // increase the usage count + + this-> m_UPoint-> m_iUse ;} /*************************************** ******************************** * ************/SmartPoint & SmartPoint:: operator = (const SmartPoint & tSmartPoint) {cout <"Copy operator executed" <endl; // increase the count on the right + tSmartPoint. m_UPoint-> m_iUse; // reduce the count on the left. If the value on the left is 0, release the pointer -- this-> m_UPoint-> m_iUse; if (0 = this-> m_UPoint-> m_iUse) {delete this-> m_UPoint;} // indicates the shortest copy, pointing to the same copy this-> m_UPoint = tSmartPoint. m_UPoint; return * this ;}

Test code

Int main (int argc, char * argv []) {class SmartPoint * ASmartPoint = new SmartPoint (); ASmartPoint-> SetName ("Class "); ASmartPoint-> ShowInfo (); // copy and construct the class SmartPoint * BSmartPoint = new SmartPoint (* ASmartPoint); BSmartPoint-> ShowInfo (); class SmartPoint * CSmartPoint = new SmartPoint (); // copy and construct * CSmartPoint = * BSmartPoint; ASmartPoint-> ShowInfo (); delete ASmartPoint; BSmartPoint-> ShowInfo (); delete BSmartPoint; CSmartPoint-> ShowInfo (); delete CSmartPoint; char c; cout <"output any key to exit ......... "<endl; c = getchar (); return 0 ;}

Test Results

650) this. width = 650; "title =" qq 30928203606.jpg "src =" http://www.bkjia.com/uploads/allimg/131228/193A0N34-0.jpg "alt =" 203708544.jpg"/>

This article is from the "Feng qing yang song" blog, please be sure to keep this source http://2309998.blog.51cto.com/2299998/1303180

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.