C ++ virtual functions - virtual table - virtual pointer - Polymorphism - how polymorphism - pure virtual function - abstract class

              / -> Class -> virtual tables / Create

In virtual constructor

             \ -> Object -> Virtual Pointer \ initialization 

Polymorphism of class: virtual functions and is based on late binding (runtime) implemented 

 

1. The function is called with the virtual keyword declared virtual function , virtual function must have the class member functions .

2. The presence of a virtual function of the class has a dimensional virtual function table called the virtual table . Object class virtual table has a pointer to the start of a virtual pointer . And the virtual table is corresponding to a class, and is a virtual table pointer corresponding to the object.
3. Polymorphism is a variety of interfaces to achieve , it is object-oriented core. Polymorphism Polymorphism is divided into classes and functions.
4. Polymorphic implemented with virtual functions, in conjunction with dynamic binding.
5. A pure virtual function is a virtual function together = 0.
Pure virtual function: virtual void breathe () = 0 ; i.e. abstract class! It must be implemented in sub-class of this function! That is, first names, not content, for content in a derived class!
6. The abstract class means that class includes at least one pure virtual function.

We look at an example: Example. 1 l-
#include <, iostream.h>
class Animal
{
public:
       void SLEEP ()
       {
              COUT << "Animal SLEEP" << endl;
       }
       void  Breathe ()
       {
              COUT << "Animal Breathe "<< endl;
       }
};

class fish:public animal
{
public:
       void breathe()
       {
              cout<<"fish bubble"<<endl;
       }
};

void main()
{
       fish fh;
       animal *pAn=&fh; // 隐式类型转换
       pAn->breathe();

}

 

Note that no virtual functions defined in Example 1-1 procedure. Consider what the result of the implementation of the program in Example 1-1 is?

The answer is the output: animal breathe

 

We main () function defines first object fh in a class of fish, then the pointer variable pAn defines a class of pointing animal, fh will assign the address of the pointer variable pAn, and then use the variable call pAn-> breathe ( ). Many students tend to this situation and C ++ polymorphism confused that fh is actually a class of target fish, fish should be calling class breathe (), the output of "fish bubble", then the result is not the case. The following two ways to tell us why.
1, the angle of compiled
C ++ compiler at compile time, to determine the function called for each object ( required for this function is non-virtual ) address, which is called early binding (Early Binding) , when we object class fish when fh address assigned pAn, C ++ compiler type conversion , then C ++ compiler thinks variable pAn saved is the address of the target animal. When performing pAn-> breathe () in main () function is called, of course, is a function of animal breathe objects.
2, the angle of the memory model
we give the fish the object memory model, as shown below:

Virtual function - virtual table - virtual pointer - Polymorphism - how polymorphism - pure virtual function - abstract class (rpm) - QiQi Ha er ~ - QiQi Ha er ~ a blog

FIG. 1-1 fish memory model class object
when the object class we construct fish, animal calls the first class constructor to construct an object class animal, fish and then call the constructor of the class to complete their part of the structure, so that the splicing a complete fish objects. When we target fish class is converted to animal type, the object is considered to be the upper half of the original objects in the entire memory model, which is in Figure 1-1 "animal memory occupied by objects." So when we use the object pointer type conversion to call its methods, of course, it is to call the method in which it is in the memory. Therefore, the output of animal breathe, will come out ahead.
As many students think, in Example 1-1 program, we know pAn actually points to an object class of fish, we hope that the results output of fish is breathing method, that method invocation breathe like fish. This time, turn to virtual functions in relation debut.

The results of the previous output is because the compiler at compile time, it has been determined that the address of the function object to call, to solve this problem we must use late binding (late binding) technology. When the compiler uses late binding, it will be determined at run time to go to the correct type of object and call the function. And let the compiler uses late binding, use the virtual keyword when declaring a function in the base class (note that this is necessary, because many of them do not use virtual functions and write many wrong example), this function we call a virtual function. Once a function is declared as virtual in the base class, the function is virtual in all derived classes without the need to explicitly declared as virtual . (However, in order to detect the code late, you should add the virtual keyword in the derived class)

 

Example 1-1 The following code is modified, the animal class Breathe () function is declared virtual, as follows:
Example 2 l-
#include <, iostream.h>
class animal
{
public:
       void SLEEP ()
       {
              COUT << "animal SLEEP "<< endl;
       }
       Virtual  void  Breathe ()
       {
              COUT <<" Animal Breathe "<< endl;
       }
};


class FISH: public Animal
{
public:
       void  Breathe ()
       {
              COUT <<" Bubble FISH "<< endl ;
       }
};

void main ()
{
       FISH FH;
       animal *pAn=&fh;
 // implicit type conversion
       PAN-> Breathe ();
}

We can run the program again, you will find that the result is "fish bubble", according to the type of object that is calling the correct function.
So when we breathe () is declared as virtual, behind what happened then?
The compiler at compile time, have found that animal class virtual function, then the compiler class comprises a virtual function for each create a virtual table (i.e. the vtable), the table is a one-dimensional array stored in the array address for each virtual function. For the procedure of Example 1-2, Animal and fish class contains a virtual function Breathe (), so the compiler will have to establish a virtual table for these two classes, (even if there is no virtual subclass function, but its parent there are, therefore also has subclasses) as shown below:

Virtual function - virtual table - virtual pointer - Polymorphism - how polymorphism - pure virtual function - abstract class (rpm) - QiQi Ha er ~ - QiQi Ha er ~ a blog

                                                    FIG. 1- 2 animal and fish-based class virtual table


So how do you locate the virtual table it? The compiler also provides a pointer to a virtual table (i.e. vptr), the pointer points to the object's class virtual table for each object class. The program is running, according to the type of object to initialize vptr, so that the correct point vptr their class virtual table, so that when you call a virtual function, we can find the right function. For the procedure of Example 1-2, since the actual object pointed pAn type fish, so fish vptr point of the vtable of the class, when calling pAn-> when Breathe (), a function of the virtual address is found in the table class fish Breathe () function.


It is because each object's virtual function calls are indexed by virtual table pointer, will determine the correct initialization of the virtual table pointer is very important. In other words, before the virtual table pointer is not properly initialized, we are not able to call a virtual function. Then the virtual table pointer at any time, in any place or initialize it?


The answer is to create and initialize the virtual table pointer of the virtual table in the constructor. Remember to call the constructor of the order you, in the constructor class objects, first call the parent class constructor, in which case the compiler only "see" the parent does not know whether there are back after successor, it initializes virtual table pointer parent object of the parent class virtual table pointer to a virtual table. The constructor is executed when the subclass, the subclass virtual table pointer object is initialized to point to its virtual table. For the procedure of Example 2-2, when the fish fh class object construction is completed, the internal virtual table pointer is initialized to point will fish class virtual table. After conversion type, call pAn-> breathe (), since pAn fish actually points to the object class, the object inside the virtual table pointer is fish class virtual table, so that the final call of class Breathe fish ( )function.


To Note: For the virtual function call, the inside each object has a virtual table pointer, the virtual table pointer is initialized to the present class virtual table. Therefore, in the program, regardless of the type of object you how to convert, but the interior of the object virtual table pointer is fixed, so, in order to achieve dynamic object function call, which is the principle of polymorphism in C ++ implementation.
Summary (base class with a virtual function):


1. Each class has a virtual table.
2. virtual table can be inherited, if the subclass does not override the virtual function, then the address sub-class virtual table will still be of the function, but this address points to a base class virtual function to achieve. If there are three base class virtual function, then the base class virtual table have three (virtual function address), will have a derived class virtual table, at least three, if the corresponding virtual function override, then the virtual table the address will change, pointing to its virtual functions to achieve. If the derived class has its own virtual function, then the virtual table will be added.
Table 3. virtual derived class virtual function address in the same arrangement order and arrangement of the base class virtual function table of the virtual addresses.
 


This is the C ++ polymorphism. When the C ++ compiler at compile time, found breathe animal class () function is a virtual function that will be used when the C ++ late binding (late binding) technology. That is not to determine the specific function calls at compile time, but at run time, depending on the type of the object (in the program, address the class object of our fish pass) to confirm which function is invoked, this ability is called C ++ polymorphism. We're not breathe () function when added before the virtual keyword, C ++ compiler at compile time determines which function is called, it is called early binding (early binding).

 

Virtual functions are defined in the base class, the object of the specific behavior is undefined its derived classes.

Example:

Define a base class: class Animal // animal. Its function breathe () // respiratory
define a second class class Fish // fish. It also functions as breathe ()

Then define a class class Sheep // sheep. It also functions as breathe ()

 

To simplify the code, Fish, Sheep Animal defined as the base class of the derived class.
However, Fish and Sheep's breathe is not the same, one to breathe through the water in the water, a direct breathing air. Therefore class may be defined to determine how to breathe, so that the base class only defines a virtual breathe, it is an empty virtual function. With this function are defined in the subclass. Procedures are generally run, find the class, if it is a base class, find its base class, the last operation is a function of the base class, then, it is found in the base class virtual function is identified, it will go back to find the same name in the subclass function. Derived class is also called a subclass. Base class, also known as the parent class. This is reflected polymorphisms result virtual function, and the like (Breathe) a.

Polymorphism herein refers to the class polymorphism.
Polymorphisms function refers to a function is defined as a function of many different parameters, they generally exist in the header file, when you call this function, for different parameters, it will call different functions of the same name. Example: Rect () // rectangle. Its parameters can be two coordinate points (point, point) may also be four coordinates (x1, y1, x2, y2 ) reload function is called polymorphism and function.

Polymorphism of class with a virtual function and means to achieve the delay bound. Polymorphisms function is overloaded function.

Under normal circumstances (not involving virtual functions), when we use a pointer / reference calls a function, the function is called depends on the
type of pointer / reference. That is, if the pointer / reference is a base class object pointer / base class is called a reference; if the pointer / reference is derived object pointer / reference is called a derived class methods, of course, if the derived class is not this method, it is It will be up to the base class to find the appropriate method. These calls at compile time is determined.

When designing the polymorphism when using virtual functions and dynamic binding, but this time the call will not be determined at run time is determined at compile time. Type is not considered a separate pointer / reference type but by the pointer / reference object to determine the function call, the function call to determine which virtual object address pointer to the virtual function table.

Guess you like

Origin blog.csdn.net/smartgps2008/article/details/90745011