In the previous article, we introduced how to disable the C ++ heap object. Today, we will introduce how to disable C ++ stack objects in the same logical area of the memory. I hope that you will have a deep understanding of the application skills in this area.
- C ++ basic concepts of time
- Detailed explanation of C ++ timing operations
- Introduction to C ++ pre-processing commands
- Introduction to logical partition of C ++ memory
- How to disable the C ++ heap object
When a C ++ stack object is created, the top pointer of the stack is moved to a space of an appropriate size. Then, the corresponding constructor is called to form a stack object, when the function returns, it will call its destructor to release this object, and then adjust the stack top pointer to reclaim the stack memory. In this process, operat or newdelete operations are not required. Therefore, setting operator newdelete to private cannot be achieved. Of course, from the above description, you may have thought of setting the constructor or destructor as private, so that the system cannot call the constructor, of course, you cannot generate objects in the stack.
This is indeed true, and I plan to adopt this solution. But before that, we need to make it clear that if we set the constructor to private, we cannot use new to directly generate heap objects, because new will also call its constructor After allocating space for the object. Therefore, I plan to only set the destructor to private. In addition to limiting the generation of stack objects, does setting the Destructor private affect the generation of stack objects? Yes, this also limits inheritance.
If a class is not intended as a base class, the usual solution is to declare its destructor as private.
To restrict C ++ stack objects, but not restrict inheritance, we can declare the Destructor as protected, so that the two are both beautiful. The following code is used:
- Class NoStackObject
- {
- Protected
- ~ NoStackObject (){}
- Public
- Void destroy ()
- {
- Delete this; call to protect destructor
- }
- };
Next, you can use the NoStackObject class as follows:
- NoStackObject hash_ptr = new NoStackObject ();
- ...... Operate on the object pointed to by hash_ptr
- Hash_ptr-destroy ();
Is it a bit strange? We use new to create an object, but instead of delete to delete it, we use the destroy method. Obviously, users are not used to this weird method of use. So I decided to set the constructor to private or protected. This goes back to the problem that we tried to avoid, that is, we don't need new. How can we generate an object? We can do this indirectly by providing a static member function for this class to generate heap objects of this type. The singleton mode in the design mode can be implemented in this way .) Let's take a look:
- Class NoStackObject
- {
- Protected
- NoStackObject (){}
- ~ NoStackObject (){}
- Public
- Static NoStackObject creatInstance ()
- {
- Return new NoStackObject (); call the protected Constructor
- }
- Void destroy ()
- {
- Delete this; Calls protected destructor
- }
- };
Now we can use the NoStackObject class as follows:
- NoStackObject hash_ptr = NoStackObjectcreatInstance ();
- ...... Operate on the object pointed to by hash_ptr
- Hash_ptr-destroy ();
- Hash_ptr = NULL; prevents pointer Suspension
The above is the operation method to restrict the C ++ stack object.