C ++ think notes -- Application of ruminations on C ++ in proxy class

Source: Internet
Author: User

Today's one-day course, so the study of ruminations on C ++ can only take 30 minutes at noon,

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------

 

Proxy type:

 

1> import conditions.

 

Class polymorphism is so practical in programming. We can use a pure virtual parent class pointer to operate on all its subclass objects, this makes programming easy and convenient.

 

We can easily use some template containers or custom data structures to manage a category of things (for example, we can put people of various occupations together for management .)

 

The solution is as follows ..

 

People * minutes les [100];

 

Here, people is a virtual base class. This array can be used to manage 100 instances of teacher, Doctor...

 

 

2> C ++ Solution

 

In fact, the above method can solve our problem well, but it will bring about pointer management problems, which is not promoted in C ++.

 

A good C ++ design should avoid using pointers directly. For new problems, our proxy class has emerged.

 

3> proxy

 

Our proxy class should solve two problems now. One is to close the pointer management, and the other is that it cannot affect the pure virtual parent class of the proxy.

 

The proxy class of a people is as follows .:

 

Class peoplesurrogate {

Public:

// Constructor

Peoplesurrogate (): p (0 ){}

Peoplesurrogate (const people & TP): p (Tp. Copy () {}// copy is a member function that copies itself to the people class.

 

Peoplesurrogate (const peoplesurrogate & PS): p (PS. P? PS. P-> copy: 0 ){}

// Of course, operator = () should also be implemented, which is similar to the copy structure.

~ Lelesurrogate ();

 

// Functions of people

Void look (){

P-> look ();

}

PRIVATE:

People * P;

};

 

Certificate -----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

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.