C ++ is an object-oriented language, but unlike C # and Java, it has no reflection mechanism. No reflection mechanism makes C ++ somewhat different from other languages in terms of language design, mainly reflected in intelligence. Many things have to be explicitly specified by programmers, such as the Virtual keyword to be discussed in this article. Virtual is embodied at runtime, while C ++ cannot use reflection during runtime to determine whether the parent class of the current class has this method, and whether this method is overloaded or not, therefore, you must use virtual to specify the code when writing the code, and then use the compiler to perform some special processing (that is, using virtual tables ).
We can see that the most common thing about virtual is the implementation of polymorphism in C ++.
// Dynamic_poly.h <br/> # include <iostream> <br/> // public abstract base class vehicle <br/> class vehicle <br/>{< br/> public: <br/> virtual void run () const = 0; <br/>}; <br/> // car class derived from vehicle <br/> class car: public Vehicle <br/>{< br/> Public: <br/> virtual void run () const <br/>{< br/> STD :: cout <"run a car/N"; <br/>}< br/> }; <br/> // the specific vehicle-derived class airplane <br/> class airplane: public vehicle <br/>{< br/> public: <br/> virtual void run () const <br/>{< br/> STD: cout <"run a airplane/N "; <br/>}< br/> };
// Dynamic_poly_1.cpp <br/> # include <iostream> <br/> # include <vector> <br/> # include "dynamic_poly.h" <br/> // run any vehicle <br/> void run_vehicle (const vehicle * vehicle) <br/>{< br/> vehicle-> Run (); // call the corresponding run () according to the specific type of vehicle () <br/>}< br/> int main () <br/>{< br/> Car car; <br/> airplane; <br/> run_vehicle (& Car); // call car: Run () <br/> run_vehicle (& airplane); // call airplane: Run () <br/>}
The above example comes from http://www.vckbase.com/document/viewdoc? Id = 948. This article also mentions some examples of polymorphism. The article on polymorphism is also written by Meng Yan.
In many examples of polymorphism, we can see that the methods of the base class are declared as pure virtual functions (Virtual void run () const = 0 ;), this requires that the subclass must implement this method and reflect interface-oriented programming.
I think it is easier to explain what the compiler will do after virtual is used. The following example is from http://www.cppblog.com/zhangyq/archive/2009/06/13/87597.html.
1 The compiler will add a virtual table to the virtual function of this class, similar to the following:
// Pseudo-code (not c ++, not c) for a static table defined within file base. cpp
// Pretend functionptr is a generic pointer to a generic member function
// (Remember: This is pseudo-code, not c ++ code)
Functionptr base :__ vtable [5] = {
& Base: Counter 0, & base: COUNTER 1, & base: Counter 2, & base: Counter 3, & base: Counter 4
};
2. Add a pointer to the virtual table for each class object, which is hidden.
// Your original C ++ source code
Class base {
Public:
...
Functionptr * _ vptr; supported supplied by the compiler, hidden from the programmer
...
};
3. the compiler initializes this pointer in the constructor.
Base: Base (... arbitrary Params ...)
: _ Vptr (& base: :__ vtable [0]) specified supplied by the compiler, hidden from the programmer
...
{
...
}
In a derived class, it also adds a hidden virtual table, but it can overrides the virtual functions of the base class such:
// Pseudo-code (not c ++, not c) for a static table defined within file der. cpp
// Pretend functionptr is a generic pointer to a generic member function
// (Remember: This is pseudo-code, not c ++ code)
Functionptr der ::__ vtable [5] = {
& Der: Limit 0, & der: Limit 1, & der: Limit 2, & base: Limit 3, & base: limit 4
};
From the above Code we can easily draw out the structure of the class, of course, you can use/d1reportsingleclasslayout to show the layout of the class (http://blog.csdn.net/chief1985/archive/2009/10/23/4720191.aspx ).
Virtual inheritance and virtual destructor are also used.
The following is an example of virtual inheritance from http://blog.csdn.net/wuliming_ SC /archive/2009/01/31/3855607.aspx:
Class point2d {<br/> Public: <br/> //... <br/> protected: <br/> float _ x; <br/> float _ y; <br/>}; <br/> class vertex: public Virtual point2d {<br/> Public: <br/> //... <br/> protected: <br/> vertex * Next; <br/>}; <br/> class point3d: Public Virtual point2d {<br/> public: <br/> //... <br/> protected: <br/> float _ z; <br/>}; <br/> class vertex3d: Public vertex, public point3d {<br/> public: <br/> //... <br/> protected: <br/> float mumble; <br/> };
Generally, the use of virtual inheritance involves diamond inheritance (C ++ allows multiple inheritance). In this case, if virtual inheritance is not used, the following situations may occur:
1. duplicate creation of common base-class sub-objects.
2. Name Conflict of member functions
3. Name Conflict of data members
The appearance of virtual destructor is also in the case of polymorphism. If you do not use a virtual destructor, The subclass destructor will not be called. For example, refer to explain.