C + + Contemplative learning note Sixth chapters handle (reference counting pointer embryonic?) )

Source: Internet
Author: User

A simple point-coordinate class

Class Point {
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;
};

Bind the handle to it on a layer of encapsulation

Controlling those operations through handle exposes those operations that are private to prevent users from manipulating the address of a point

It is more important to provide a reference count of point to bind multiple handle to Ppint to release the right to use the point

Operation to release point

Then add two classes

A reference count class Upoint

Class Upoint {
Friend class Handle;
Point P;
int u;

Upoint (): U (1) {}
Upoint (int x, int y):p (x, Y), U (1) {}
Upoint (const point& p0):p (P0), U (1) {}
};

One is that the handle handle class contains the pointer construction and destruction of Upoint when the count is +1

Release the pointer memory when counting to 0

Class Handle {
Public
Handle ();
Handle (int, int);
Handle (const point&);
Handle (const handle&);
handle& operator= (const handle&);
~handle ();
int x () const;
handle& x (int);
int y () const;
handle& y (int);
Private
upoint* up;
};

The full code is as follows:

1 //sample0.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 7 classPoint {8  Public:9Point (): Xval (0), Yval (0){}TenPoint (intXinty): Xval (x), Yval (y) {} One     intX ()Const{returnxval;} A     intY ()Const{returnYval;} -point& x (intXV) {xval = XV;return* This; } -point& y (intYV) {yval = YV;return* This; } the Private: -     intxval, Yval; - }; -  + classUpoint { -FriendclassHandle; + Point p; A     intu; at  -Upoint (): U (1) {} -Upoint (intXintY):p (x, Y), U (1) {} -Upoint (Constpoint& p0):p (P0), U (1){} - }; -  in classHandle { -  Public: to Handle (); +Handle (int,int); -Handle (Constpoint&); theHandle (Consthandle&); *handle&operator=(Consthandle&); $~Handle ();Panax Notoginseng     intX ()Const; -handle& x (int); the     intY ()Const; +handle& y (int); A Private: theupoint*Up ; + }; -  $Handle::handle (): Up (Newupoint) {} $Handle::handle (intXintY): Up (Newupoint (x, y)) {} -Handle::handle (Constpoint& p): Up (NewUpoint (P)) {} -  thehandle::~Handle () { -     if(Up->u = =0)Wuyi delete up; the } -  WuHandle::handle (Consthandle& h): Up (h.up) {++up->u;} -handle& Handle::operator=(Consthandle&h) { About++h.up->u; $     if(--up->u = =0) { - delete up; -     } -     return* This; A } +  the intHandle::x ()Const{returnUp->p.x ();} - intHandle::y ()Const{returnUp->p.y ();} $  thehandle& handle::x (intx0) the { theUp->p.x (x0); the     return* This; - } in  thehandle& Handle::y (inty0) { theUp->p.y (y0); About     return* This; the } the  the  + intMain () - { the Handle H;BayiStd::cout << h.x () <<" "<< h.y () <<Std::endl; the     return 0; the}
View Code

C + + Contemplative learning note Sixth chapters handle (reference counting pointer embryonic?) )

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.