C ++ Shen Shuographic reading note (2) handle class.

zhaozj2021-02-12  148

C Shen Shuographic reading note (2) handle class.

Continuation "C Shen Shuographic Reading Notes (1) Agent" ******** Question ******* Create a proxy will copy the object you need, for some classes, copy the price Too big or contain resources that cannot be easily copied. How to avoid copying the cost of copying the object while maintaining the polymorphism of the agent

Class Point {// A simple class, the function without a pointer member often does not need to rewrite the assignment operator and copy constructor public: Point (): xval (0), yval (0) {} Point (int x, int y : xval (x), yval (y) {} int x () const {return xval;} int y () const {return yval;} Point & x (int xv) {xval = xv; return * this;} Point & Y (int yv) {yval = yv; return * this;} private: int xval, yval;

******** Solution ******** @ Handle class's contour defines an appropriate Handle class, binding the Handle class to the objects they control: 1. Handle class should be created And destroy the object to create its own Point object and pass it to a handle to pass the parameters of the creation point to Handle2. Access the Point object over the Handle class to overload the Handle class's Operator-> operator, expose the internal Point object (this article is not used This way) clearly selects the Handle class to support those Point operations

Class handle {public: handle (); handle (int, int); handle (const pangle&); handle & operator = (const Handle &); ~ Handle (); int x () const; handle & x INT); int y () const; handle & y (int); private: // ...};

@ 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽 聽聽G It must be known that the location (more can't use Static member) with all other handles of the same object (not to use Static member) reference count cannot be part of the object, requiring no rewrite the existing Point object

Class Upoint {// Used to store reference counts and Point object private: // purely designed for implementation, all members have a private class handle; point p; int U;

Upoint (): u (1) {} // Upoint object always generates with the HANDLE class, so the default reference count is 1 UPoint (INT X, INT Y): P (x, y), u (1) {} UPOINT (Const Point & P0): P (P0), U (1) {};

Class handle {public: handle (); handle (int, int); handle (const pangle&); handle & operator = (const Handle &); ~ Handle (); int x () const; handle & x int y () const; handle & y (int); private: Upoint * Up; // New Adding}; @ Handle Class Implementation // Constructor Handle :: Handle (): Up (New Upoint) {} Handle :: Handle (INT X, INT Y): Up (New Upoint (x, y) {} handle :: handle (const point & p): Up (new Upoint (p)) {}

// Destructor Handle :: ~ Handle () {IF (Up-> u == 0) delete Up;}

// The copy constructor only increases the reference count Handle :: Handle (Const Handle & H): Up (H.UP) { Up-> U;}

// Assignment operator may cause the original left object counter to 0// Call first to ensure that the left and right sides can work when the same UPOINT object can also work Handle & Handle :: Operator = (Const Handle & H) { H .UP-> u; if (--up-> u == 0) delete Up; Return * this;}

// Simple read function int Handle :: x () const {return up-> p.x ();} int handle :: y () const {return up-> p.y ();

// Complex write function / * handle class Handle H (3, 4) // former objects, Handle H2 = H; // Copy H2.x (5); // Modify Copy Object INT N = HX (); // 3 or 5? * /

/ / Pointer semantic: After copying the handle, copy the object and the original object use the same UPOINT object

Handle & Handle :: X (INT X0) {UP-> P.X (X0); Return * this;}

Handle & Handle :: Y (int y0) {UP-> P.Y (Y0); return * this;}

// Value Semizist: After copying the handle class, the copy object and the original object do not use the same UPOINT object //, the object replication is performed when absolutely necessary, called write on write, and the copy is only targeted. One optimization technology of the object

Handle & Handle :: X (INT X0) {if (Up-> U! = 1) {--up-> u; Up = new Upoint (Up-> P);} Up-> PX (X0); Return * THIS;

Handle & Handle :: Y (int y0) {if (Up-> u! = 1) {--up-> u; up = new upoint (up-> p);} Up-> py (Y0); return * THIS;

转载请注明原文地址:https://www.9cbs.com/read-6958.html

New Post(0)