"C + + Meditation record" fifth chapter: the use of C + + proxy class __c++

Source: Internet
Author: User
use of C + + proxy classes

This blog is my 5th chapter in the study of C + + meditation notes.

This article mainly talked about the use of proxy classes in C + + and the use of.

The so-called proxy class, that is, surrogate. Why use it, simply for example.

1 class Vehicle
2 {
3 Public:
4 Vehicle () {}
5 Virtual String getName () = 0;
6}:

8 Class Car:public Vehicle
9 {
Ten public:
One car () {}
virtual String GetName () {}
13};

Class Bike:public Bike
16 {
Public:
Bike () {}
virtual String GetName () {}
20}:

There are simple 3 classes, and the inheritance relationship is simple.

If you want to define a parkinglot now, how do you save it?

Some would say that it would be nice to have a vehicle array directly. namely vehicle parkinglot[500];

But there's going to be a big mistake here!

First of all, obviously vehicle is a virtual base class, and virtual base classes do not have objects.

Secondly, the formulation of the array cannot show any polymorphic properties. (polymorphism can only be represented by pointers and references)

There is also a way of writing is vector<vehicle*> Parkinglot.

This is also true. But there is the hassle of dynamic memory management. And if the following code appears

void Wrong_code (vector<vehicle*> &parkinglot)
{
Car C;
Parkinglot.push_back (&C)//insert C into Parkinglot
}

int main ()
{
Vector<vehicle*> Parkinglot;
Wrong_code (Parkinglot);
Parkinglot[0].getname (); oops!! The address of the Parkinglot memory points to a destroyed memory

}

can cause the program to run when the runtime appears error!

In this premise, the proxy class on the homeopathy and born.

As the name suggests, a proxy class is a proxy for a base class and its subclasses, and its function is to enable it to display polymorphism in the container as well. Without the annoyance of dynamic memory management.

Now define the proxy class vehiclesurrogate for vehicle and subclasses.

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
Ten * Revision:none
One * COMPILER:GCC
12 *
Author:summer (), marchtea213@gmail.com
Company:
15 *
16 * =====================================================================================
17 */
18
19/*
20 * =====================================================================================
* Class:vehicle
* Description:
23 * =====================================================================================
24 */
25
#include <iostream>
#include <string>
28
using namespace Std;
30
Class Vehicle
32 {
Public:
34
Vehicle () {}
Virtual String getName () = 0;
Notoginseng virtual vehicle* copy () const = 0;
Virtual ~vehicle () {}//virtual destructor is to support polymorphism. But this example does not need.
Private:
40
41
42};
43
Class car:p ublic Vehicle
45 {
Public:
The car () {}
\ Virtual String GetName () {return ' car ';}
Virtual vehicle* copy () const {return new car;}
Virtual ~car () {}
51
52
53};
54
Class Bike:public Vehicle
56 {
Public:
Bike () {}
Virtual String GetName () {return "bike";}
Virtual vehicle* copy () const {return new Bike;}
Virtual ~bike () {}
62};
63
Vehiclesurrogate class
65 {
Public:
Vehiclesurrogate ():p (0) {}
Vehiclesurrogate (const vehicle& v):p (V.copy ()) {}
Vehiclesurrogate (const vehiclesurrogate &vs):p (VS.P? Vs.p->copy (): 0) {}
Vehiclesurrogate & operator= (const vehiclesurrogate &AMP;VS);
A string GetName () {return p->getname ();}
~vehiclesurrogate () {delete p;}
Private:
Vehicle *p;
75};
76
Vehiclesurrogate & vehiclesurrogate::operator= (const VEHICLESURROGATE&AMP;VS)
78 {

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.