Mysteria - Virtual Mechanism (on)

zhaozj2021-02-16  56

Summary: C is a rural zone, virtual mechanism is very important, but very dangerous C features, so it is necessary to explain this, hoping to help everyone.

What is virtual function briefly, that is, in the member function, add keyword Virtual so that this member function becomes a virtual function. The idea of ​​virtual functions is from Simula to calculate the most significant feature in C . The virtual function allows the implementation of the derived class replacement base class. The compiler ensures that the implementation of the derived class is always called when the object is derived, even if the object is an access to the base class pointer instead of derived pointers. Why should we use the virtual function to be added in the virtual function, people have a strong inspiration to this stuff, there is a common statement that the virtual function is just a function of a functions, which is completely redundant. What's more, saying that good design does not require those scalable and openness provided by virtual functions. After these views, there is another critical critique: a virtual function value is not a low efficiency form. For this master, the masters launched a war, the road is very twists and turns, I don't intend to repeat these points, if you are very interested, you can refer to: The D & V of C , TC Pl, what is OOP Here, I probably talk about his importance: From the object-oriented point of view, if there is no virtual function, C can't be an object-oriented. Although the overload is very good, don't forget, it is just the syntactic decoration of a structural pointer to the function in C concept; although the standard library contains many templates to achieve the same very good "generic programming" technology, The function is still using C , object-oriented programming, through the virtual function in the Override base class in the subclass, can achieve an important feature of OO - polymorphism. From a business perspective, if there is no virtual function, C is not object-oriented. Naturally, we have no reason to go to C from C to C . If there is no object-oriented, we don't have enough reasons to train developers, develop new tools. If we only have the syntax of the C class without object-oriented, it will not reduce maintenance costs, and actually increase the training cost. From a language perspective, there is no virtual function of C is not object-oriented, and the class programming without dynamic binding can only be "object-based" instead of "object-oriented". Abandoning the virtual function, actually abandoning OO! The result became an early ADA language. What is the different non-virtual member function of virtual functions and non-virtual functions is static, in other words, the member function will be static when compiling. However, the virtual member function is dynamically determined, in other words, the member function is dynamically selected at runtime, which is based on the type of object, rather than pointing to the type of the object or the type of reference. This is called "dynamic binding". Most compilers use some of the following techniques: If the object has one or more virtual functions, the compiler places a hidden pointer into the object, which is called VPTR. This VPTR points to a global watch that is called VTBL. When you distribute a virtual function, the runtime system follows the VPTR of the object to find the class of VTBL, then follow the appropriate items of the appropriate item in VTBL. Space overhead of virtual function objects: Each object is an additional pointer, plus an additional pointer for each method. The time overhead of the virtual function object: and the normal function call comparison, the virtual function call requires two additional steps.

Attachment: This is not involved in the contents of many inheritance, virtual success, and does not involve the RTTI mechanism we have already said, and there is no contents such as Page Fault, through pointing to the function of the function of the function. Different virtual functions and overload have some virtual functions. It seems that the function is overloaded, but the function overload can determine the function we need to use during compilation, it is predictable; and the virtual function can run at the time Determining a specific function is unpredictable, there is a dedicated term for virtual functions ---- evening binding, using virtual functions This method is called function overlay.

The virtual function encounters inline, an interesting question, but answers are often unfinished, especially the beginners. I found that beginners generally believe that the order function cannot be inline, the reason seems to be obvious: (1) The virtual function is a mechanism for the runtime mechanism and the inline function characteristics is a compile time; (2) declare one The inline virtual function makes the program generate multiple function copies in the execution time, which will result in a lot of space waste. In fact, in many cases, virtual functions are static determined - especially when derived a way to call their base classes. You may be very strange why do you do this? The answer is simple, just two words: package. A good example is that the secting function of derived class causing the call of the destructor of the base class. In addition to the initial functions, other functions are static. If you do not sure the base class destructor is inline, this advantage cannot be played. Especially in the inheritance level, there is no doubt that the virtual function will increase the operational efficiency of the program for the virtual function when the inheritance is destructed.

Let's take an example example: Class Shape {public: inline virual void draw () = 0;}; inline void shape :: draw () {cout << "Shape :: Draw ()" << Endl;}

Class Rectangle: Public Shape {PUBLIC: Void Draw () {Shape :: Draw (); Cout << "Rectangle :: Draw () << Endl;}}; shape * p = new rectangle; P-> DRAW );

Is this DRAW inline? No, of course not. This is determined at runtime through virtual function mechanisms. This call is converted to something similar to the following: (* P-> VPTR [1]) (P); 1 represents the location of the DRAW in the virtual function list. Because this DRAW is implemented by the function pointer _vptr [1], the compiler cannot compile the time to determine the address of the call function, so the function cannot be inline. Of course, the definition of the inline virtual function DRAW must appear in a certain place to ensure proper operation of the execution code call. That is, at least one definition needs to place its address in the virtual function list. How does the compiler determine when to build that definition? A method is to generate a definition when generated in a virtual function list. This means generating a virtual function list for each instance of each class. Examples of each inline function are also generated. How many virtual functions are you want to generate in an executable program? Well, although the standard has made some regulations on the behavior of the virtual function; but there is no constraint for the implementation. Because the virtual function list is not specified in the standard. So obviously does not specify how to control the virtual function list or how much instance is generated. In addition, the C standard now requires the inner function performance as a definition of only one inline function in a program, even if the function is defined in different files. The new provision is that the implementation is generated as only one instance. Potential problems involving code expansion will also disappear when this feature is widely implemented.

Not finished (to be renewed ...)

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

New Post(0)