C ++ virtual function table polymorphic - realization polymorphism: virtual function & vtable

 
 C ++ interview frequently asked question is multi-state principle. If the understanding of the nature of the object-oriented C ++ is not particularly good, ask here will collapse. The following basic principles to, elaborate multi-states: & virtual function virtual function table.
 

1. The polymorphic nature:

Formally, the use of a unified parent to make a general pointer processing. But the actual implementation, this pointer may point to a subclass object.

Formally, the original call to the parent class, but in fact calls the subclass method of the same name.

Frankly, polymorphism through pointers is to use the parent class, parent class and subclass method can call their own. If not polymorphic, for subclasses called with the parent class pointer, also called the method of the parent class.

DETAILED Reference: C ++ virtual function table polymorphic - polymorphism simple use

 

【note】

When program execution, a pointer to the parent class parent class object, or sub-class objects, is not formally resolved. Only through a multi-state mechanisms to perform real corresponding method.

 

2. virtual functions:

Before the method of the parent class function, you can increase the virtual function becomes the virtual function, such as:

Note that, with the example inline functions, when the package to the outside, particularly the former method implemented without adding virtual, with the error.

1  class Father
 2  {
 3  public :
 4      virtual  void play ()              // parent class's play () method before increasing the virtual keyword, this function has become the virtual function 
5      {
 6          std :: cout << " This is a parent class the Play " << STD :: endl;
 . 7      }
 . 8  };
 . 9  
10  class Son: public Father
 . 11  {
 12 is  public :
 13 is      void Play ()
 14      {
 15          STD :: << COUT" This is a subclass of Play " << STD :: endl;
 16      }
 . 17 };

 

3. virtual functions inheritance:

If a member function is declared virtual, so subclasses [it] in a derived class inherited member functions, virtual functions will become.

If override this virtual function in a subclass, can not write virtual, it is recommended to write virtual, it would be more readable codes, such as 13 rows:

. 1  class Father
 2  {
 . 3  public :
 . 4      virtual  void play ()                                         // parent class play () method to increase the virtual keyword before, this function will be virtual 
. 5      {
 . 6          STD :: COUT << " This is a parent class Play " << STD :: endl;
 . 7      }
 . 8  };
 . 9  
10  class Son: public Father
 . 11  {
 12 is  public :
 13 is      Virtual  void Play ()                                        // before the derived class virtual function inherited, can not add virtual, it makes the code more readable but with
 14      {
 15          STD :: COUT << " This is a subclass of Play " << STD :: endl;
 16      }
 17 };

 

 

4. The principle of virtual functions:

Principle of virtual function is achieved through virtual function tables, virtual function tables are out of the compiler do the things he does not exist with the object, look at the code below:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Father
 5 {
 6 public:
 7     virtual void func_1() { cout << "Father::func_1" << endl; }
 8     virtual void func_2() { cout << "Father::func_2" << endl; }
 9     virtual void func_3() { cout << "Father::func_3" << endl; }
10     11 };
12 is  
13 is  int main ( void )
 14  {
 15      Father father_1;         // virtual function table is stored in the object inside the father 
16  
. 17      COUT << " the sizeof (father_1) == " << the sizeof (father_1) << endl;
 18 is  
. 19 }

After running print it and see how much memory space occupied by the object father.

Operating results: sizeof (father_1) == 4

Why three virtual functions only 4 bytes? Because he is a stored table, he did not take up memory space object, the object exists in only a pointer to a virtual function table, the following schematic side, no matter how many you have virtual function, virtual function table he:

    

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

===========================================================================================================================

Guess you like

Origin www.cnblogs.com/CooCoChoco/p/12549792.html
Recommended