A virtual function is a member function declared as virtual in a class, and when the compiler sees this type of function invoked by a pointer or reference, it performs a late binding on it, that is, the type information of the class to which the pointer (or reference) points, determines which class the function is. Usually such a pointer or reference is declared as a base class, and it can point to an object of a base class or derived class.
Polymorphism means that the same method can behave differently according to the different objects to which it belongs.
Here's an example:
Program run output results: 8, 12
Why is the base object size 8 bytes Instead of 4 bytes, and why is the size of the derived object 12 bytes instead of 8 bytes, and what is the extra 4 bytes? What does it have to do with the implementation of polymorphism?
Each class with a virtual function (or a derived class of a class with a virtual function) has a virtual function table with a pointer to a virtual function table in any object of that class. The virtual function address of the class is listed in the virtual function table. The extra 4 bytes are used to put 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 which you place the address of all virtual functions declared as virtual in this class or in its base class. The compiler then places the vptr in this class to point to the corresponding vtable. Vptr must be initialized in the constructor, and the virtual function cannot be invoked until Vptr is initialized. All of the base class objects or objects derived from the base class are vptr in the same place as the respective objects. All vtable have the same order, regardless of the type of object.
The C + + function call is the same as are all from right to left, in which case the value of the first address of the object, the this pointer, is pushed into the stack, because this must be pushed into the stack as a parameter when calling each member function, so the member function knows which particular object it is working on. In this way, we can always see that the number of times the stack is pressed before the member function call equals the number of arguments plus one (except for the static member function, which does not have this).
Pure virtual function:
An abstract class is to precede the declaration of a class with the virtual keyword, in order to prevent misuse of abstract classes, you can define pure virtual functions in an abstract class, such as virtual void X () = 0;
Doing so is to tell the compiler to keep an interval for the function in vtable, but the address is not placed at this particular interval, as long as there is a pure virtual function, the vtable is incomplete, and a class containing pure virtual functions is called a pure abstract base class.
Virtual functions are the mechanisms used in C + + to implement polymorphism (polymorphism). The core idea is to access the function defined by the derived class through the base class. Let's say we have the following class hierarchy:
So, in use, we can:
A * a = new B ();
A->foo (); Here, A is a pointer to a, but the called function (foo) is B!
This example is a typical application of a virtual function, in which case you may have some idea of a virtual function. It is virtual on the so-called "delayed" or "dynamic", the invocation of a class function is not determined at compile time, but is determined at run time. Because it is not possible to write code that is called a function of a base class or a derived class, it becomes a "virtual" function.
The following declaration indicates that a function is a pure virtual function:
Class A
{
Public
virtual void foo () = 0; =0 flag a virtual function as pure virtual function
};
When a function is declared as pure virtual, the pure virtual function means: I am an abstract class! Don't take me out of my case! A pure virtual function is used to regulate the behavior of a derived class, which is actually called an "interface". It tells the user that my derived class will have this function.