The principle of virtual function in C++



Member functions in a class are divided into static member functions and non-static member functions, and non-static member functions are further divided into ordinary functions and virtual functions.

Q: Why use virtual functions

A: With virtual functions, we can get good scalability. In a well-designed object-oriented program, most functions communicate with the interface of the base class. Because when using the base class interface, the program calling the base class interface can adapt to the new class without changing. If the user wants to add new functionality, he can inherit from the base class and add the corresponding new functionality.

Q: Briefly describe the role of C++ virtual functions and the underlying implementation principle

A: The main point is to answer the role of the virtual function table and the virtual function table pointer.

Virtual functions are used to implement dynamic binding.

The virtual function in C++ is implemented using the virtual function table and the virtual function table pointer. The virtual function table is the address table of the virtual function of a class, which is used to index the address of the virtual function of the class itself and the parent class. If the subclass rewrites the parent class virtual function, the corresponding virtual function will be replaced by the address of the function of the subclass in the virtual function table (the subclass may not be a virtual function, but must have the same name); the virtual function table pointer exists in each object ( Usually, for efficiency reasons, it will be placed at the start address of the object), which points to the address of the virtual function table of the class where the object is located; in a multiple inheritance environment, there will be multiple virtual function table pointers, respectively pointing to different base classes. virtual function table.

The virtual function table is one for each class (with virtual functions). The virtual function table pointer is one for each object.

Only virtual functions can be stored in the virtual function table, and ordinary functions cannot be stored.

If a function is not virtual, the call to it (that is, the address of the function) is determined at compile time. Calling a virtual function (its address) cannot be determined until runtime.

The function entry of a virtual function is dynamically bound. At runtime, the program calls the function of the corresponding version of the object according to the actual object pointed to by the base class pointer. (Use the object's virtual function table pointer to find its virtual function table, and then call a different function.) (This is only done if it is a virtual function (use the virtual function table pointer to look up the virtual function table). Non-virtual function Just call your own.)

follow up: http://www.cppentry.com Programming Development Programmers Getting Started

Why do you need virtual destructors? (When should a virtual destructor be used?)

When there is class inheritance and some resources need to be destructed in the destructor, the destructor needs to be a virtual function. Otherwise, if the parent class pointer is used to point to the child class object, only the destructor of the parent class will be called during delete, but the destructor of the child class cannot be called, resulting in a memory leak.

Which is faster for an object to access ordinary member functions or virtual functions?

It is faster to access ordinary member functions, because the address of ordinary member functions has been determined at the compilation stage, so the function of the corresponding address is directly called when accessing;

and when a virtual function is called, it is necessary to first find the address of the virtual function in the virtual function table. , so it is slower than ordinary member functions.

Does a destructor have to be a virtual function?

uncertain. 1. The efficiency of virtual functions is relatively low; 2. Some classes do not have subclasses, so there is no need to use virtual destructors.

Can inline functions, constructors, static member functions be virtual functions?

Neither can.

The inline function (inline) needs to be expanded at the compile stage (it has been determined at compile time), while the virtual function is dynamically bound at runtime and cannot be expanded at compile time, so it is contradictory;

When the constructor is called, there is no concept of parent class and subclass. The parent class will only call the constructor of the parent class, and the subclass will call the subclass, so there is no concept of dynamic binding (the parent class can only have the concept of dynamic binding. Subclass, when the superclass is constructed, the subclass does not yet exist, and it is impossible for the subclass to dynamically call the subclass in the superclass);

can the virtual function be called in the constructor?

Yes, but it is meaningless, and it will not have the effect of dynamic binding. The constructor of the parent class still calls the function of the parent class, and the function called in the child class is still the function of the child class.

Briefly describe the role of virtual inheritance in C++ and the underlying implementation principle?

Virtual inheritance is used to solve the diamond-shaped inheritance problem under the condition of multiple inheritance. The underlying implementation principle is related to the compiler, and is generally implemented through virtual base class pointers, that is, only one object is stored in each object. The object of the parent class is referenced through the virtual base class pointer during multiple inheritance, so as to avoid the ambiguity problem in the diamond inheritance.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445303&siteId=291194637