The last time, this problem is solved with a polymorphism, and the practice of transitioning the pointer type is just mentioned. Later, I thought that this explanation is unsatisfactory, because the premise with polymorphism is that multiple objects that want to put in must be derived from one base class, and must have a unified interface. This also illustrates why there must be a public base class in an object-oriented design, any class must be derived from it, even if you only write a "Hello World". Too dead, isn't it?
However, anyone has considered no circumference, suppose now your program is halfway, suddenly found, you need to put it originally unrelated (just because I think they are not related) objects into a container (array) , Linked list ...), is there a modification of the inheritance relationship? - Of course, you can use multiple inheritance, you need to put it on the object, then set up a housing; however, I suggest you don't do this, the reason is that you will be more complex, often unexpected things, There is no need to find trouble for this application, you can take the following practices, you can think of it as a polymorphism that is artificial:
//----------------------------Timer.h---- -----------------------
#ifndef Timer_H
#define Timer_H
#include
Class Timer
{
PUBLIC:
Timer () {QueryperFormanceFrequency (& FREQUENCY);
Inline void start () {queryperformancecounter (& Timerb);
Inline Double GetTime ()
{
QueryperFormanceCounter (& Timere);
Return (Timere.quadpart - Timerb.quadpart) / (Double) Frequency.quadpart * 1000.0;
}
Private:
Large_integer Timerb, Timere, Frequency;
}
#ENDIF
//-----------------------------------Timer.h End ------- ----------------
#include
#include
#include "timer.h"
Using namespace std;
Class A // Using multiple inherited housings
{
PUBLIC:
Virtual void Run () = 0;
}
Class A1: Public A
{
PUBLIC:
Void Say () {cout << "i am class a1." << endl;}
Void Run () {SAY ();
}
Class A2: Public A
{
PUBLIC:
Void Speak () {cout << "I am Class A2." << endl;}
Void Run () {SPEAK ();
}
Class A1A2 / / A1, A2
{
PUBLIC:
A1A2 (A1 * P): PA1 (P), TYPE (TA1) {}
A1A2 (A2 * P): PA2 (P), Type (TA2) {}
Void Run ()
{
Switch (Type)
{
Case TA1: PA1-> Say (); Break;
Case TA2:
PA2-> Speak (); Break;
DEFAULT: BREAK;
}
}
Private:
Union {A1 * PA1; A2 * PA2;
ENUM {TA1, TA2};
INT TYPE;
}
Void m1 () // "Polymorphism" using manual methods
{
Timer D; D.Start (); Vector
For (i = 0; i <4; i )
{
B.PUSH_BACK (A1A2 (New A1 ()));
B.PUSH_BACK (A1A2 (New A2 ()));
}
// for (i = 0; i <8; i ) b [i] .Run ();
Cout << D.gettime () << endl;
}
Void M2 () // Method of multiple inheritance
{
Timer D; D.Start (); Vector b; int i;
For (i = 0; i <4; i )
{
B.PUSH_BACK (New A1 ());
B.PUSH_BACK (New A2 ());
}
// for (i = 0; i <8; i ) B [I] -> Run ();
Cout << D.gettime () << endl;
}
int main ()
{
M1 (); m2 ();
Return 0;
}
In fact, here A1, A2 are both base classes, but we can assume that they are all derived. When you need to use a polymorphism method, you must add the housing on their inheritance relationship, so It is multiple inherited, I think you will not pick me this. The implementation of artificial methods, the principle of polymorphism is like, which is based on the type in which the pointer is actually stored, and the corresponding class is called.
After knowing the time function, it is like addiction, always test it. Artificial "polymorphism" method and polymorphic method which is faster? I don't listed data, the test shows that manual methods are fast than the system's polymorphism, and VC6 is about 0.8 times faster, and BCB6 is about 0.3 times. The polymorphism of BCB6 is faster than VC6, but manual methods are slower than VC6. I use it as little as a short cycle, and it is estimated that this impact is not big.
Even if this method is more fast than the system, I still don't encourage this method to replace the polymorphism, unless you use it on C (there is no way, it is no way). Because programming is awarded process, you are difficult to expect what happens, using polymorphism and this requirement, you can predetermined a frame in advance. And this method is a summary process, for the latter changes, must be modified at any time, very similar to the current programs, is a supplement to the previous idea. So the two methods are not intercept each other, but complement each other.