In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What this article shares with you is about how constructors and destructors are used in C++. The editor thinks it is very practical, so I share it with you. I hope you can learn something after reading this article. Let's take a look at it with the editor.
1. Constructors and destructors do not return values?
Reason: the creation and destruction of objects in the program is a very special thing, there is a compiler to call them to complete, and here the constructor and destructor are the creation and destruction of the two functions, they are called by the compiler as hook functions.
It is precisely because of this that they do not need to have a return value, because once there is a return value, the compiler must know how to deal with the return value, and whether the compiler has a way to make a decision, it can only be operated by the programmer. this conflicts with the calling mechanism of constructors and destructors, so constructors and destructors do not return values.
two。 When polymorphic, why does the destructor need to be a virtual function?
Of course, when we are polymorphic, we can not define the constructor as a virtual function, which can also be compiled and run, and there is no problem specifying that inherited classes are created and destroyed.
But what is the purpose of achieving polymorphism? In fact, the purpose is to create an inherited class, but the pointer is the base class. When we use this base class pointer, we can execute the corresponding function of the inherited class while the program is running. For common examples, you can refer to the factory model.
Since we want to use the pointer of the base class to represent the inherited class, in this case, if the destructor is not a virtual function, there may be a problem. When the base class pointer destructor is called, it will not release that part of the data of the inherited class itself. Will cause this part of the data to not be released.
3. Why can't constructors be virtual functions?
This has something to do with the implementation mechanism of polymorphism. the realization of polymorphism to virtual function is to instantiate a virtual function pointer to a class object, and then point the virtual function pointer to the list of virtual functions, so as to realize polymorphism.
The constructor is called first when the class object is instantiated, so once a class has an object, the constructor is called, that is to say, when the constructor is called, there is no object generated, there is no virtual function pointer, and the function address of the virtual function is stored in the list of virtual functions. The constructor has finished executing and the result has not yet found the position in the list of virtual functions corresponding to this virtual function.
4. Why can't constructors and destructors call virtual functions?
The reason why the constructor cannot use virtual is similar to the reason in 3, except that when executing the constructor, if the virtual function is executed in the constructor, there is no way to find the list of virtual functions through the virtual pointer of this object. Because the class object has not been created at this time, the virtual function pointer cannot be found.
When the destructor of an inherited class is called, the class variables in the object are considered to be undefined values, so there is no way to find the corresponding virtual function list, and of course there is no way to find the corresponding function address in the virtual function list. After entering the destructor of the base class, this class is considered to be the object of the base class, so the virtual function actually has no meaning, because the virtual function actually corresponds to the function in the base class, and there is no way to realize the role of polymorphism.
The above is how constructors and destructors are used in C++. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.