Agent notes in C ++

Source: Internet
Author: User

Introduction of proxy classes

The reason for proxy is that the client cannot communicate with the server normally and the third party needs to communicate. This third party naturally becomes a proxy. To be precise, it should be a proxy on the server, the class used to implement the proxy function is called the proxy class.

When can the client and server fail to communicate normally? C ++ meditation Note: C ++ containers can only contain one type of objects. So how can we design a C ++ container so that it can contain objects of different types and related to each other? The first thing that comes to mind is whether the object stored in the container is itself, but a pointer to the object. Although this seems to have solved the problem, there are two disadvantages: first, the pointer to the object is stored, not the object itself, in this way, when the object is destructed and the pointer is not deleted, it becomes a wild pointer. Second, the storage pointer increases the extra burden of memory allocation. Based on this, we can solve the above problem by defining an object named surrogate.

Problem

Example:

Assume that the following hierarchy represents different types of transportation tools:

 Class  Vehicle {  Public  :  Virtual   Double Weight () Const =0  ;  Virtual   Void Start () = 0  ;};  Class Roadvehicle: Public  Vehicle {};  Class Autovehicle: Public  Roadvehicl {};  Class Aircrate: Public  Vehicle {}; Class Helicopter: Public  Aircraft {}; 

To track and process a series of different types of vehicle, the definition is as follows: Vehicle parking_lot [1000]. This does not achieve the expected results, because vehicle is a virtual base class, it is impossible to have an object, or its object array may not exist.

Classic Solution

Since the above object cannot exist, an indirect layer is provided, and the easiest thing is to store the pointer to the object, that is, vehicle * parking_lot [1000]. But as mentioned above, there are two defects, the biggest defect is that the pointer is easy to be a wild pointer. The reason for being a wild pointer is that the source object to which the pointer is directed has been destructed. To solve this problem, the parking_lot array is not a pointer to the original object, instead, they point to their copies, such:

 
Helicopter X; parking_lot [num_vehicles++] =NewHelicopter (X );

And adopt a convention: When parking_lot is released, all objects pointed to in it are also released.

The pointer pointing to the object copy adds the burden of dynamic memory management.

Introduce proxy Solutions

The relationship between the proxy class and the original class vehicle: 1. The behavior is similar to the original class vehicle; 2. It potentially represents all objects inherited from the virtual base class vehicle.

To potentially represent all objects that inherit from the virtual base class vehicle, the use of virtual functions is used to process unknown objects in C ++. To copy vehicle of any type, add a suitable virtual function in the vehicle class:

 
ClassVehicle {Public:Virtual DoubleWeight ()Const=0;Virtual VoidStart () =0;VirtualVehicle * Copy ()Const=0;Virtual~Vehicle (){}};

For example, the copy function of helicopter is:

 
Vehicle * helicopter: Copy ()Const{Return NewHelicopter (*This);}

Now define the proxy class:

 
 

Class vehiclesurrogate
{
Public:
Vehiclesurrogate ();
Vehiclesurrogate (const Vehicle &);
Vehiclesurrogate (const vehiclesurrogate &);
~ Vehiclesurrogate ();
Vehiclesurrogate & operator = (const vehiclesurrogate &);

 
 

Double weight () const;
Void start ();

PRIVATE:

Vehicle * VP;

};

Define the constructor vehiclesurrogate (const Vehicle &) in the proxy class to create a proxy for any objects that inherit the vehicle class. The proxy class also has a default constructor, so you can create an array of vehiclesurrogate objects. However, the default constructor has a problem. How can we define the default operation of vehiclesurrogate? What is the object type it points? It cannot be vehicle, because vehicle is an abstract base class and there is no vehicle object at all. So introduce the defined Behavior

The concept of a zero-pointer empty proxy (empty surrogate) allows you to create, destroy, and copy such a proxy, but other operations are considered as errors.

The following is the definition of the proxy class member function:

Vehiclesurrogate: vehiclesurrogate (): VP ( 0  ) {} Vehiclesurrogate: vehiclesurrogate (  Const Vehicle & V): VP (V. Copy () {} vehiclesurrogate: vehiclesurrogate (  Const Vehiclesurrogate & V): VP (V. VP? V. VP-> copy (): 0  ) {} Vehiclesurrogate & Vehiclesurrogate :: Operator = ( Const Vehiclesurrogate & V ){  If ( This ! = & V) {Delete VP; VP = (V. VP? V. VP-> copy (): 0  );}  Return * This  ;} Vehiclesurrogate :: ~ Vehiclesurrogate () {Delete VP ;} Double Vehiclesurrogate: weight () Const  {  If (Vp = 0  ){  Throw   "  Empty vehiclesurrogate. Weight ()  "  ;}  Return VP-> Weight ();}  Void  Vehiclesurrogate: Start (){ If (Vp = 0  ){  Throw   "  Empty vehiclesurrogate. Start ()  "  ;} VP -> Start ();} 

Defined as a proxy class, you can define parking_lot [1000:

 
Vehiclesurrogate parking_lot [1000]; Helicopter X; parking_lot [num_vehicle++] = X;

The last statement is equivalent:

 
Parking_lot [num_vehicle ++] = vehiclesurrogate (X );

Summary

Sharing inheritance with containers forces two problems to be handled: 1. Controlling memory allocation; 2. Placing different types of objects in the same container.

Each object of the proxy class represents another object, which can be an object of any class in a complete inheritance level. You can use a proxy object instead of the object itself in the container.

 

Reference

1. Chapter 5 agency in C ++ meditation

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.