C ++ meditation book note (Chapter 6)-handle: Part 1

Source: Internet
Author: User

 

When a proxy is created, the objects on the proxy will be copied. How can this replication be avoided?

You can use the handle class. The handle class is similar to the smart pointer class. By using the * and-> operators of the overload handle class, the handle class can be transformed into a smart pointer.

In terms of effect, handle is a container that only contains a single object. It avoids copying by allowing multiple handle objects to point to the same object.

Define the handle class. We also need to define a new class to accommodate the reference count of the referenced class object and the referenced class object itself.

The sample code is as follows:

# Include <iostream>

Using namespace std;

Class Point

{

Private:

Int xval, yval;

Public:

Point (): xval (0), yval (0)

{

Cout <"Point" <endl;

}

Point (int x, int y): xval (x), yval (y)

{

Cout <"Point" <endl;

}

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;

}

~ Point ()

{

Cout <"~ Point "<endl;

}

};

Class UPoint // This class is invisible to users and is an indirect layer.

{

Friend class Handle;

Point p; // Point Object

Int u; // counter

 

UPoint (): u (1) // provides the construction method of all points

{

Cout <"UPoint:" <u <endl;

}

UPoint (int x, int y): p (x, y), u (1)

{

Cout <"UPoint:" <u <endl;

}

UPoint (const Point & p0): p (p0), u (1)

{

Cout <"UPoint:" <u <endl;

}

~ UPoint ()

{

Cout <"~ UPoint: "<u <endl;

}

};

Class Handle

{

Private:

UPoint * up; // deal with the indirect layer UPoint

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 );

};

 

Handle: Handle (): up (new UPoint)

{

Cout <"Handle:" <up-> u <endl;

}

 

Handle: Handle (int x, int y): up (new UPoint (x, y) // construct handle, handle-> UPoint-> Point

{

Cout <"Handle:" <up-> u <endl;

}

 

Handle: Handle (const Point & p): up (new UPoint (p) // create a copy of a Point

{

Cout <"Handle:" <up-> u <endl;

}

 

Handle ::~ Handle ()

{

If (-- up-> u = 0)

{

Cout <"~ Handle: "<up-> u <endl;

Delete up;

}

Else

{

Cout <"~ Handle: "<up-> u <endl;

}

}

 

Handle: Handle (const Handle & h): up (h. up)

{

+ Up-> u; // handle is copied here, but the underlying point object is not copied, just reference count plus 1

Cout <"Handle:" <up-> u <endl;

}

 

Handle & Handle: operator = (const Handle & h)

{

+ H. up-> u; // Add 1 to the object reference count on the right and 1 to the left

If (-- up-> u = 0)

Delete up;

Up = h. up;

Cout <"Handle:" <up-> u <endl;

Return * this;

}

 

Int Handle: x () const

{

Return up-> p. x ();

}

 

Int Handle: y () const

{

Return up-> p. y ();

}

 

Handle & Handle: x (int x0) // assume that the Handle is Pointer semantics.

{

Up-> p. x (x0 );

Return * this;

}

 

Handle & Handle: y (int y0)

{

Up-> p. y (y0 );

Return * this;

}

 

/*

Handle & Handle: x (int x0) // assume that the Handle is a value semantics (copy at write time)

{

If (up-> p! = 1)

{

-- Up-> u;

Up = new UPoint (up-> p );

}

Up-> p. x (x0 );

Return * this;

}

 

Handle & Handle: y (int y0)

{

If (up-> p! = 1)

{

-- Up-> u;

Up = new UPoint (up-> p );

}

Up-> p. y (y0 );

Return * this;

}

*/

 

Void f (Handle h) // test the function. Copy the handle, but not copy the Point.

{

Cout

}

 

Int main ()

{

Handle h (3, 5 );

Cout

F (h );

 

Point o (10, 9 );

Handle a (o );

Handle B ();

Return 0;

}

 

Author: yucan1001

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.