In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of C++ polymorphism and virtual function instance analysis, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe that you will gain something after reading this C++ polymorphism and virtual function instance analysis article. Let's take a look.
The usual interview sites for C++
Although Ali is the largest Java manufacturer in China, not all business is supported by Java. Many services and middle-and lower-level storage, computing, network services, and large-scale distributed tasks are written by C++. Of all the departments in Ali, the one who inspects C++ most deeply is probably Ali Yun.
Ali's frequent test points for C++:
Related implementation of 1.STL container
2. Understanding of the new features of Clipper +
3. Realization of Polymorphism and Virtual function
4. The use of pointers
II. Ali Real question 2.1 Real question one
Now suppose there is a compiled C++ program, there is no error, but the runtime error, the error is as follows: you are calling a pure virtual function (Pure virtual function call error), what may be the cause of this error?
Pure virtual function call errors are generally caused by the following reasons:
Call the virtual function directly from the base class constructor. (direct call means that the virtual function is called directly inside the function.)
Call the virtual function directly from the base class destructor.
Call the virtual function indirectly from the base class constructor. (indirect call means that other non-virtual functions are called within the function, and the virtual functions are called directly or indirectly inside the function)
Call the virtual function indirectly from the base class destructor.
Call a virtual function through a dangling pointer.
Note: this type of error is detected by the 1BI 2 compiler. The compiler cannot detect such a situation and will report an error at run time.
(1) Virtual function table vtbl
The compiler creates a virtual function table for each class with virtual functions at compile time
When an object is instantiated, the compiler automatically points the virtual table pointer of the class object to the virtual function table.
(2) the process of constructing a derived class object
1. Construct the base class section:
Construct a virtual table pointer to point the virtual table pointer of the instance to the vtbl of the base class
Construct the member variables of the base class
Execute the constructor function body of the base class
two。 Recursively construct derived classes:
Point the virtual table pointer of the instance to the derived class vtbl
Construct member variables of a derived class
Execute the constructor body of a derived class
(3) the process of destructing a derived class object
1. Recursive destructor derived class part:
Point the virtual table pointer of the instance to the derived class vtbl
Execute the destructor body of a derived class
Destruct the member variables of a derived class (here the execution function body, the destructor derived class member variables, the order and construction steps of the two are opposite)
two。 Destructing base class part:
Point the virtual table pointer of the instance to the vtbl of the base class
Execute the destructor function body of the base class
Member variables of the destructor base class
When the constructor and destructor execute the function body, the virtual function table pointer of the instance points to the virtual function table of the class to which the constructor and the destructor itself belong, and the virtual function executed at this time, that is, the virtual function of the called class itself, here is a [indirectly called] chestnut: in the destructor in the base class, the pure virtual function is called (the virtual function is defined in the base class).
# include using namespace std;class Parent {public: / / Pure virtual function virtual void virtualFunc () = 0; void helper () {virtualFunc ();} virtual ~ Parent () {helper ();}}; class Child: public Parent {public: void virtualFunc () {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.
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.