C ++ proxy

Source: Internet
Author: User

 

Use of C ++ proxy classes

The so-called proxy class is surrogate. Why should we use it? A simple example is as follows.

1 class Vehicle
2 {
3 public:
4 vehicle (){}
5 virtual string getname () = 0;
6 }:
7
8 class car: Public Vehicle
9 {
10 public:
11 car (){}
12 virtual string getname (){}
13 };
14
15 class bike: Public bike
16 {
17 public:
18 bike (){}
19 virtual string getname (){}
20 }:

There are three simple classes, and the inheritance relationship is also very simple.

If we want to define a parkinglot, how can we store it?

Some people will say that it is better to directly use a Vehicle array, that is, Vehicle parkinglot [500];

But there will be a big mistake here!

First of all, Vehicle is a virtual base class, and the virtual base class does not have objects.

Secondly, the writing of this array cannot show any polymorphism. (polymorphism can only be reflected by pointers and references)

Another method is vector <Vehicle *> parkinglot.

This method is also possible, but it may be difficult to manage the dynamic memory, and the following code appears:

Void wrong_code (vector <Vehicle *> & parkinglot)
{
Car c;
Parkinglot. push_back (& c); // insert c to parkinglot
}

Int main ()
{
Vector <Vehicle *> parkinglot;
Wrong_code (parkinglot );
Parkinglot [0]. getName (); // Oops !! The address of parkinglot memory points to a piece of memory destroyed.
 
}

Runtime error may occur when the program is running!

 

On this premise, the agent class is born.

As the name suggests, a proxy class is a proxy of a base class and its sub-classes. Its function is to make it show polymorphism in the container, without the worry of dynamic memory management.

Now we define Vehicle and the subclass's proxy class VehicleSurrogate.

1 /*
2 * = ========================================================== ========
3 *
4 * Filename: surrogate. cpp
5 *
6 * Description: chapter 5 in book
7 *
8 * Version: 1.0
9 * Created: 12/04/2011 07:04:12 AM
10 * Revision: none
11 * Compiler: gcc
12 *
13 * Author: Summer (), marchtea213@gmail.com
14 * company:
15 *
16 * = ========================================================== ========
17 */
18
19 /*
20 * = ========================================================== ========
21 * Class: Vehicle
22 * description:
23 * = ========================================================== ========
24 */
25
26 # include <iostream>
27 # include <string>
28
29 using namespace std;
30
31 class Vehicle
32 {
33 public:
34
35 Vehicle (){}
36 virtual string getName () = 0;
37 virtual Vehicle * copy () const = 0;
38 virtual ~ Vehicle () {}// virtual destructor are used to support polymorphism, but this example does not need to be used.
39 private:
40
41
42 };
43
44 class Car: public Vehicle
45 {
46 public:
47 Car (){}
48 virtual string getName () {return "car ";}
49 virtual Vehicle * copy () const {return new Car ;}
50 virtual ~ Car (){}
51
52
53 };
54
55 class Bike: public Vehicle
56 {
57 public:
58 Bike (){}
59 virtual string getName () {return "bike ";}
60 virtual Vehicle * copy () const {return new Bike ;}
61 virtual ~ Bike (){}
62 };
63
64 class VehicleSurrogate
65 {
66 public:
67 VehicleSurrogate (): p (0 ){}
68 VehicleSurrogate (const Vehicle & v): p (v. copy ()){}
69 VehicleSurrogate (const VehicleSurrogate & vs): p (vs. p? Vs. p-> copy (): 0 ){}
70 VehicleSurrogate & operator = (const VehicleSurrogate & );
71 string getName () {return p-> getName ();}
72 ~ VehicleSurrogate () {delete p ;}
73 private:
74 Vehicle * p;
75 };
76
77 vehiclesurrogate & vehiclesurrogate: Operator = (const vehiclesurrogate &)
78 {
79 if (this! = & VS) // you must remember to judge whether Vs and this are the same before deleting P. Otherwise, a problem may occur.
80 {
81 Delete P;
82 p = vs. P-> copy ();
83}
84 return * this;
85}
86
87
88
89 int main ()
90 {
91 car C;
92 bike B;
93
94 vehiclesurrogate VS1 (C );
95 std: cout <vs1.getName () <std: endl;
96 VehicleSurrogate vs2 (B );
97 std: cout <vs2.getName () <std: endl;
98
99 return 0;
100
101}

Therefore, Vehicle is transformed to support copying to our proxy class.

The program running results are also clear, as we expected.

Car

Bike.

The code is very simple and can be clearly understood without explanation.

Obviously, with the proxy class, parkinglot is not hard to define.

This method also simplifies the trouble of dynamically managing memory in the case of vector <Vehicle *>.
Postscript:

When we see the advantages of the proxy class, we must also face up to its shortcomings. that is to say, replication is required every time you use it. For some very large classes, replication is not a wise choice.

Next I will introduce the handle class to solve this problem.

 

Source: blog

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.