In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "the generation and prohibition of C++ stack objects". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
When creating a C++ stack object, the pointer at the top of the stack will be moved to "free" the appropriate size of the space, and then the corresponding constructor will be called directly in this space to form a stack object, and when the function returns, its destructor will be called to release the object, and then adjust the top pointer to retrieve the stack memory. The operator newdelete operation is not required in this process, so setting operator newdelete to private does not achieve the goal. Of course, you may have thought from the above description: make the constructor or destructor private so that the system cannot call the constructor and, of course, cannot generate objects on the stack.
This is indeed possible, and I intend to adopt this scheme. But before that, there is one thing to consider, that is, if we make the constructor private, we cannot use new to generate the heap object directly, because new also calls its constructor after allocating space for the object. So, I'm going to just set the destructor to private. Further, does setting the destructor to private have any other impact besides limiting stack object generation? Yes, it also limits inheritance.
If a class is not intended to be a base class, the usual approach is to declare its destructor as private.
To restrict C++ stack objects but not inheritance, we can declare the destructor as protected, which has the best of both worlds. The following code is shown:
Class NoStackObject {protected ~ NoStackObject () {} public void destroy () {delete this; call protection destructor}}
Next, you can use the NoStackObject class like this:
NoStackObject hash_ptr = new NoStackObject ();... Manipulate the object pointed to by hash_ptr hash_ptr-destroy ()
Isn't it a little weird that we use new to create an object, but instead of using delete to delete it, we use the destroy method. Obviously, users are not used to this strange way of using it. So I decided to set the constructor to private or protected as well. This goes back to the question that I tried to avoid above, that is, without new, how to generate an object? We can do this indirectly by having this class provide a static member function specifically for generating this type of heap object. The singleton pattern in the design pattern can be implemented in this way. (let's take a look:
Class NoStackObject {protected NoStackObject () {} ~ NoStackObject () {} public static NoStackObject creatInstance () {return new NoStackObject (); call protected constructor} void destroy () {delete this; call protected destructor}}
You can now use the NoStackObject class like this:
NoStackObject hash_ptr = NoStackObjectcreatInstance ();... Manipulate the object pointed to by hash_ptr hash_ptr-destroy (); hash_ptr = NULL; prevent the use of the dangling pointer "the way C++ stack objects are generated and disabled". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.