What is the memory structure of virtual functions in C++?
Editor to share with you what the memory structure of virtual functions in C++ is like, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article. Let's learn about it!
Memory structure of virtual function
I. basic concepts
The function of virtual function in C++ is mainly to realize the mechanism of polymorphism.
With regard to polymorphism, in short, you use a pointer of the parent type to point to an instance of its subclass, and then call the member function of the actual subclass through the pointer of the parent class. This technique allows the pointer of the parent class to have "multiple forms", which is a generic technique.
The so-called generic technology, to put it bluntly, is to try to use immutable code to implement variable algorithms. For example: template technology, RTTI technology, virtual function technology, either try to make a decision at compile time, or try to do a run-time resolution.
2. the definition of virtual function and the redefinition in derived class
Class class name
{
Public:
Description of virtual member function
}
Class class name: access modifier base class name
{
Public:
Description of virtual member function
}
Third, the structure of virtual function in memory
1. Let's look at an example first.
# include
Using namespace std
Class A
{
Public:
Virtual void fun0 () {cout