A brief analysis of virtual functions in C++

  • 2020-04-02 01:28:07
  • OfStack

A, definitions,
Definition: a member function declared virtual in a base class and redefined in one or more derived classes.

Syntax: virtual function return type function name (parameter table) {function body}

Purpose: to achieve polymorphism, by pointing to the base class pointer to the derived class, access to the derived class with the same name override member function virtual function must be a non-static member function of the base class, access can be protected or public.

A function defined as virtual is one that the base class expects derived classes to redefine, and the base class expects derived classes to inherit functions that cannot be defined as virtual functions.

Second, the role of
The function of virtual function is to realize dynamic linkage, that is, to dynamically select the appropriate member function in the running stage of the program. After defining the virtual function, the virtual function can be redefined in the derived class of the base class. The redefined function in the derived class should have the same number and type of parameters as the virtual function. To achieve uniform interface, different definition process. If a virtual function is not redefined in a derived class, it inherits the virtual function of its base class. When the program finds the keyword virtual in front of the virtual function name, it is automatically treated as dynamic linking, that is, the appropriate member function is dynamically selected while the program is running. Virtual functions are a manifestation of C++ polymorphism.

With virtual functions, we have the flexibility to do dynamic binding at a cost of course. If the function (method) of the parent class is not necessary or impossible to implement and is completely dependent on the subclass to implement, we can set this function (method) to virtual function name =0. We call such function (method) pure virtual function. If a class contains pure virtual function, we call this class abstract class.

We just set the member function of the base class to virtual, and the corresponding function of its derived class will automatically become virtual.

Iii. Dynamic binding process
At the surface point, a virtual function is the virtual version of a function defined by the object to which it is called based on the type of the object to which it is called by the pointer or reference.

Since there are virtual functions in the class, the compiler inserts a piece of data for the class that you don't know about and creates a table for it. That data is called a VPTR pointer to that table. That table is called VTBL, each class has its own VTBL, VTBL role is to preserve the custom is the function of virtual base class is looking forward to a derived class redefines, base class hope derived classes inherit the function cannot be defined as virtual function has the address of a virtual function in class, we can put the VTBL vividly as an array, each element of an array of this deposit is the address of the virtual function.

When the virtual function is called, the value of VPTR is firstly extracted, which is the address of VTBL. Then, according to this value, the value in the corresponding slot in VTBL is extracted, and this value is the address of the virtual function being called. Finally, the function is called. Now we can see that as long as the VPTR is different, the VTBL is pointing to different VTBL, and the VTBL contains the virtual function address of the corresponding class, so that the virtual function can do its job.

Pure virtual functions
Instead of giving a meaningful implementation of a virtual function in a base class, declare it as a pure virtual function and leave its implementation to a derived class of that base class. That's what pure virtual functions do.

A pure virtual function allows a class to have an operation name first, but no operation content, and a derived class to be defined specifically at inheritance time. Classes that contain pure virtual functions are called abstract classes. This class cannot declare an object, but serves as a base class for a derived class. A derived class also becomes an abstract class and cannot instantiate an object unless all the pure virtual functions in the base class are fully implemented in the derived class. Write = 0 after the function parameter to specify a pure virtual function.


Related articles: