Virtual FunctionsIs a member function declared as virtual in the class. When the compiler sees that such a function is called through a pointer or reference, it is bound to it later, that is, through a pointer (or reference) the type information of the class to which the function belongs. Generally, such pointers or references are declared as base classes. They can refer to objects of base classes or derived classes.
Polymorphism means that the same method can have different behaviors based on different objects.
Here is an example:
Program running output: 8, 12
Why is the Base object 8 bytes instead of 4 bytes? Why is the size of the Derived object 12 bytes instead of 8 bytes? What is the use of the four extra bytes? What is the relationship with the implementation of polymorphism?
Each class with a virtual function (or a class derived class with a virtual function) has a virtual function table, and any object of this class has a pointer to the virtual function table. The virtual function address of the class is listed in the virtual function table. The four extra bytes are used to store the address of the virtual function table.
Whenever you create a class that contains a virtual function or derive a class from a base class that has a virtual function, the compiler creates a VTABLE for this class. In this table, placed in this class or in its base class, the address of the virtual function is voiced as virtual. The compiler then places the VPTR in this class to point to the corresponding VTABLE. VPTR must be initialized in the constructor. virtual functions cannot be called before VPTR initialization. The VPTR of all base class objects or objects derived from the base class are in the same position of the respective objects. All vtables have the same sequence, regardless of the type of objects.
Function calls in C ++, like C, are pushed from the right to the left. In this case, the first address of the object, that is, the value of this pointer, is pushed to the stack, this is because each member function must be called as a parameter to be pushed to the stack, so the member function knows which special object it works on. In this way, we can always see that the number of times the stack is pressed before the member function call is equal to the number of parameters plus one (except for the number of STatic member functions, it does not have this ).
Pure virtual functions:
An abstract class is added with the virtual keyword before the class Declaration. To prevent misuse of the abstract class, you can define pure virtual functions in the abstract class, such as virtual void x () = 0;
In this way, the compiler is told to reserve an interval for the function in VTABLE, but the address is not put at this specific interval. If there is a pure virtual function, the VTABLE is incomplete, classes that contain pure virtual functions are called pure abstract base classes.
Virtual functions are used in C ++ to implement polymorphism. The core concept is to access the functions defined by the derived class through the base class. Suppose we have the following class layers:
In this case, we can:
A * a = new B ();
A-> foo (); // here, although a points to A, the called function (foo) is B!
This example is a typical application of virtual functions. Through this example, you may have some concepts about virtual functions. It is virtual on the so-called "Deferred Association" or "Dynamic Association". The call of a class function is not determined at the time of compilation, it is determined at the runtime. When writing code, you cannot determine whether the base class function or the derived class function is called. Therefore, it becomes a "virtual" function.
The following statement indicates that a function is a pure virtual function:
Class
{
Public:
Virtual void foo () = 0; // = 0 indicates that a virtual function is a pure virtual function.
};
After a function is declared as pure virtual, the pure virtual function means: I am an abstract class! Don't instantiate me! Pure virtual functions are used to regulate the behavior of derived classes. They are actually called "interfaces ". It tells the user that my Derived classes will all have this function.