In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the matters needing attention when implementing C++ virtual functions? in view of this problem, this article introduces the corresponding analysis and answers in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible way.
C++ in the role of C++ virtual function is mainly to achieve a polymorphic mechanism, virtual function (Virtual Function) is through a virtual function table (Virtual Table) to achieve, referred to as V-Table, for everyone to learn and reference!
If a class does not contain virtual functions, this often indicates that it is not intended to be used as a base class. When a class is not intended to be a base class, it is often a bad idea to declare the destructor as virtual. Consider a class that represents points in two-dimensional space:
Class Point {/ / a 2D point public: Point (int xCoord, int yCoord); ~ Point (); private: int x, y;}
If an int occupies 32 bits, a Point object applies exactly to 64-bit registers. Moreover, such a Point object can be passed as a 64-bit quantity to a function written in other languages, such as C or FORTRAN. If Point's destructor were virtual, the situation would be completely different.
The implementation of C++ virtual functions requires the object to carry additional information that is used at run time to determine which virtual function the object should call. Typically, this information takes the form of a pointer called vptr (virtual table pointer, virtual function table pointer).
Vptr points to an array of function pointers called vtbl (virtual table, virtual function table), and each class that contains C++ virtual functions is associated with vtbl. When an object calls a virtual function, the actual called function is determined by the following steps: find the vtbl that the object's vptr points to, and then find the appropriate function pointer in the vtbl.
The details of how virtual functions are implemented are unimportant. What is important is that if the Point class contains a virtual function, the size of objects of this type will increase. In a 32-bit architecture, they will grow from 64-bit (equivalent to two int) to 96-bit (two int plus vptr)
In a 64-bit architecture, they may grow from 64-bit to 128-bit, because the pointer size is 64-bit in such an architecture. Adding vptr to Point will increase its size by 50-100%! The Point object is no longer suitable for 64-bit registers. Also, the Point object is in C++ and other languages such as C #.
It no longer seems to have the same structure because other languages lack equivalents for vptr. As a result, it is no longer possible for Points to pass in or out of functions written in other languages unless you make an explicit correspondence for vptr, which is its own implementation detail and therefore loses portability.
The benchmark here is that indiscriminately declaring all destructors as virtual is as wrong as never declaring them virtual. In fact, many people have summed up this rule: declare a virtual destructor if and only if the class contains at least one virtual function.
However, when there is no C++ virtual function at all, it may bite with the problem of non-virtual destructor. For example, the standard string type does not contain C++ virtual functions, but misguided programmers sometimes use it as a base class:
SpecialString * pss = new SpecialString ("Impending Doom"); std::string * ps;. Ps = pss; / / SpecialString* = > std::string*... Delete ps; / / undefined! In practice, / / * ps's SpecialString resources / / will be leaked, because the / / SpecialString destructor won't / / be called
At first glance, this may seem harmless, but if somewhere in the program, for some reason, you transform a pointer to SpecialString into a pointer to string, and then you apply delete to the string pointer, you are immediately sent into undefined territory.
On the implementation of C++ virtual functions to pay attention to which questions and answers are shared here, I hope the above content can be of some help to you, if you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.