Read Inside the C ++ Object Model

zhaozj2021-02-16  55

Today, I saw the Pointer-to-Member Functions section, and when the virtual function, the function pointer of the member function under multi-inheritance is given, the StrousTrup program is given: add a structure to record the difference between the pointer, to ordinary members Function, the acquisition is the address of the member function, and the virtual function gets the serial number of the function in the virtual function table. Then mention a program of Microsoft, that is, use the Thunk function (this word is not very determined to translate, although it is understandable, it is probably similar to the "Pile" function.): For a pointer to ordinary member functions, with the original None difference; for virtual function pointers, it is a pointer called "vcall", which is responsible for actual virtual function calls by the Thunk function. Its scheme still uses the structure introduced by Stroustrup, but removes index in the structure (pointing to the index value of the virtual function table, but also determines whether it is a virtual function pointer), thereby reducing memory consumption; while alleviating each time An additional judgment when the function pointer is called (determined whether it is a virtual function pointer): The current ordinary member function call does not have additional overhead; and the virtual function call is replaced by an indirect function call, which is basically It is flat to the original. This is actually a typical example in which an indirect layer solves problem is introduced. "Any questions that can be solved by introducing an indirect layer" in the introduction of an indirect layer (this example is itself solving the problem encountered in the OO implementation, but the solution itself is not related to OO). It can be seen that the idea is in communication, but OO thinking is not only available in the OO field, but also possible in other areas.

转载请注明原文地址:https://www.9cbs.com/read-18304.html

New Post(0)