Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the C++ virtual function table?

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

C++ virtual function table is what, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Preface

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.

I will not elaborate too much on the use of virtual functions here. You can take a look at the relevant books on C++. In this article, I just want to give you a clear analysis of the implementation mechanism of virtual functions.

Of course, some of the same articles have appeared on the Internet, but I always feel that these articles are not easy to read, with large chunks of code, no pictures, no detailed instructions, no comparisons, and no examples. It is not conducive to study and reading, so this is the reason why I want to write this article. I also hope that you can give me more suggestions.

To get to the point, let's enter the world of virtual functions.

Virtual function table

Anyone who knows about C++ should know that virtual functions (Virtual Function) are implemented through a table of virtual functions (Virtual Table). Referred to as V-Table for short. In this table, the main is the address table of the virtual function of a class, which solves the problem of inheritance and coverage, and ensures that it can truly reflect the actual function. In this way, in an instance of a class with a virtual function, the table is allocated to the memory of the instance, so when we use the pointer of the parent class to manipulate a subclass, the virtual function table becomes important. it is like a map, indicating the function that should actually be called.

Here we focus on this virtual function table. C++ 's compiler should ensure that the pointer to the virtual function table exists at the forefront of the object instance (every object instance of this class has a virtual function table one by one, but the address of the virtual function in the virtual function table is the same) (this is to ensure the highest performance of the virtual function table-if there are multiple layers of inheritance or multiple inheritance). This means that we get the virtual function table from the address of the object instance, and then we can traverse the function pointer and call the corresponding function.

After hearing me talk so much, I can feel that you may be more confused now than before. It doesn't matter, the following is a practical example, I believe you can understand it at a glance.

Suppose we have a class like this:

Class Base {

Public:

Virtual void f () {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report