In C + +, the object of a class is built into two types, one is static, such as a A, and the other is dynamic, such as a * ptr=new A;
Statically establishes a class object that is allocated by the compiler to the object in the stack space, by directly moving the stack top pointer, to move the appropriate space, and then call the constructor on this memory space to form a stack object. Using this method, the constructor of the class is called directly.
A class object is created dynamically by using the new operator to build the object in the heap space. This is a two-step process, the first step is to execute the operator new () function, search for the appropriate memory in the heap space and allocate it, and the second step is to call the constructor to construct the object and initialize the memory space. This method indirectly invokes the constructor of the class.
So how do you restrict a class object to be built on a heap or stack? The following are discussed separately.
1, can only be built on the heap
A class object can only be built on a heap, or it cannot statically establish a class object, that is, the constructor of a class cannot be called directly.
It is easy to think of setting the constructor to private. After the constructor is private, the constructor cannot be called outside the class to construct the class object, only the new operator can be used to build the object. However, as mentioned earlier, the execution of the new operator is a two-step process, and C + + provides overloads of the new operator, in fact, only allowing the operator new () function to be overloaded, while the operator () function allocates memory and does not provide a construction capability. Therefore, this method is not possible.
When the object is built on top of the stack, the compiler allocates memory space and calls the constructor to construct the Stack object. When the object is finished, the compiler calls the destructor to free up the space occupied by the stack object. The compiler manages the entire life cycle of an object. What would happen if the compiler could not call the class's destructor? For example, the destructor for a class is private, and the compiler cannot call the destructor to free memory. So, when the compiler allocates stack space for a class object, it checks the class's destructor for access, in fact not just the destructor, as long as the non-static function, the compiler will check. If a class's destructor is private, the compiler does not allocate memory on the stack space for the class object.
Therefore, the destructor is set to private, and the class object cannot be built on the stack. The code is as follows:
[CPP]View Plaincopyprint?
- Class A
- {
- Public:
- A () {}
- void Destory () {delete this;}
- Private:
- ~a () {}
- };
try to use A; To build an object, compile an error, and indicate that the destructor cannot be accessed. This allows the object to be built using the new operator, which is public and can be called directly. A destory function must be provided in the class to release the memory space. After the class object is used, you must call the Destory function.
One drawback of the above approach is that inheritance issues cannot be resolved. If a is a base class for other classes, the destructor is usually set to virtual and then overridden in subclasses to implement polymorphism. Therefore, destructors cannot be set to private. Fortunately C + + provides a third type of access control, protected. Setting the destructor to protected can solve this problem effectively, the protected member cannot be accessed outside the class, and the subclass can be accessed.
Another problem is that the use of classes is inconvenient, using new to create objects, but using the Destory function to dispose of objects instead of using Delete. (using delete will cause an error, because a pointer to the delete object will call the object's destructor, and the destructor is not accessible outside the class) This is a weird way to use it. For the sake of unification, you can set the constructor to protected and then provide a public static function to complete the construction, so that you do not use new, but instead use a function to construct it, using a function to deconstruct it. The code looks like the singleton pattern:
[CPP]View Plaincopyprint?
- Class A
- {
- Protected:
- A () {}
- ~a () {}
- Public:
- Static A * Create ()
- {
- return new A ();
- }
- void Destory ()
- {
- Delete this;
- }
- };
In this way, the call to create () function creates a Class A object on the heap and calls the Destory () function to free memory.
2, can only be built on the stack
Objects are built on the heap only with the new operator, so the class object can only be built on the stack as long as the new operator is disabled. Set operator new () to private. The code is as follows:
[CPP]View Plaincopyprint?
- Class A
- {
- Private:
- void * operator new (size_t t) {}//Note the first and return values of a function are fixed
- void operator delete (void * ptr) {}//reload new to overload delete
- Public:
- A () {}
- ~a () {}
- };
Resources:
1. http://www.nowcoder.com/questionTerminal/0a584aa13f804f3ea72b442a065a7618
How can I define a class that can only generate objects on the heap (on the stack)?