First, what is a virtual function
A virtual function must be a non-static member function of the class (and not a constructor) with public access. (It can also be defined as private or proteceted, but that doesn’t make sense for polymorphism.) , defines the general form of a virtual function in the class definition of the base class:
Virtual return value type Virtual function name (parameter list) {function body}
In simple terms, the member function is preceded by the virtual keyword.
1.1 When to use virtual functions?
For the designer to design a member function: 1, if the designer does not want subclasses to redefine (overwrite) a member function, make it non-virtual (ordinary member function). 2. If the designer wants a subclass to redefine (overwrite) a member function that has a default definition, make it virtual. 3. If the designer wants the subclass to have to redefine (overwrite) a member function, and there is no default definition (of course, pure virtual functions can also be defined), make it pure virtual.
Note to virtual functions:
1. The base destructor must be defined as a virtual destructor, because deleting a base pointer to a derived object produces undefined behavior if the base destructor is not.
2. C++ stipulates that when a member function is declared virtual, all derived functions of the same name automatically become virtual. Therefore, when a subclass redeclares the virtual function, it can be added or not added, but it is customary to add virtual to each layer to make the program more clear.
2, virtual pointer and virtual table (vtable)
Let’s start with the following picture:
As you can see from the table above, each instance object has its own unique virtual pointer (In the case of virtual functions in the class
) and each virtual pointer points to a virtual table (The virtual table is created during compilation. During compilation, the compiler determines the contents of the corresponding virtual table for each class.
); If a subclass and a superclass have virtual functions of the same name, only the subclass virtual address exists in the class virtual table. So the virtual pointer (Used with Pointers
) works as follows:
For example, code p->v3(), a pointer p to C example object C, p pointing to the memory area is the virtual pointer address, through the virtual pointer to find the virtual table (virtual function table), then find the corresponding v3();
Note about virtual Pointers:
1, if even a class has 100000 virtual functions, so his virtual pointer is also one, that is, no matter how many virtual functions, virtual pointer is only one;
2, the size of the virtual pointer in different bit computer is not the same, the test in the 32-bit machine is 4 bytes, in the 64-bit machine is 8 bytes;
3. Each instance object has a virtual pointer to the virtual table of each instance object, which contains a pointer to the virtual function that can be called;
C ::A::v1() or p->B::A::v1(), this behavior is called virtual function avoidance mechanism;
5. The virtual pointer approach to addressing functions is called dynamic binding;
6, the virtual pointer and the parent pointer collocation makes sense;
3, the virtual pointer function dynamic binding after the powerful function
To realize polymorphism in C++, for a simple example, look at the following figure:
All these types of pasta are inherited from the superclass pasta, so they should all have a method (member function) — the make method (which shows the member function of the make method or the member function that makes the pasta);
If we now need to go to the output of each food producing method, assuming that we input from standard input, 1000 different pasta (instance objects), we can not call in turn each of the pasta producing method, should adopt the way of circulation output at this time, we learned to inherit all know that a parent class pointer can point to a subclass, so we need only inside the loop body, Define a pointer to the superclass, pass the address of each pasta to the pointer, and call the recipe method to output the perfect recipe method for each food.
This process represents polymorphism in C++, which is polymorphism, one of the three main object-oriented features.
Only is the entire content of this article, feel don’t feel useful all point a like bai!! 😮 😮 😮