C object creation is limited to the heap or stack

Mondo Technology Updated on 2024-02-12

How do I restrict objects of a class from being created only on the heap? How do I restrict objects from being created only on the stack?

Note: There are two types of object creation for classes in C++:Static establishment, dynamic establishment

Statically established: The compiler allocates memory on the stack space for the object and directly calls the constructor of the class to create the object. For example: a a;

Dynamically established: Use the new keyword to create an object on the heap space, and the underlying layer first calls the operator new() function to find a suitable memory on the heap space and allocate it; Then, call the class's constructor to create the object. For example: a *p = new a();

Restricted objects can only be built on the heap:

The most intuitive idea: avoid calling the class's constructor directly, because when the object is statically established, the class's constructor is called to create the object. However, it is not feasible to make the constructor of a class private, because when the constructor is set to private, the constructor cannot be called outside the class to construct the object, only new to build the object. However, since the constructor of the class is also called at the bottom of the new when creating an object, after the constructor is set to private, it is not possible to create an object outside of the class with new. Therefore, this method is not feasible. Workaround 1:

Set the destructor to private。Reason: Static objects are built on the stack, and the compiler allocates and frees memory space, and when the compiler allocates memory space for the object, it checks the non-static functions of the class, that is, the compiler checks the accessibility of the destructor. When the destructor is set to private, objects created by the compiler cannot access the destructor to free up memory space for the object, so the compiler does not allocate memory for the object on the stack.

class a void destory() private: ~a()
Problems with the method:

Objects created with new usually use delete to free up the object's memory space, but the destructor cannot be called from outside the class, so a destory() function must be defined in the class to release the object created by new. There is no way to solve the inheritance problem, because if the class is used as the base class, the destructor is set to virtual, and then the function is overridden in the derived class to implement polymorphism. But at this point, the destructor is private and inaccessible in the derived class. Workaround 2:

The constructor is set to protected, and a static function of public is provided to complete the construction, instead of using the new construct outside of the class; Set the destructor to protected. Why: Similar to the singleton pattern, the destructor is guaranteed to be accessible in the derived class. Create an object on the heap by calling the create() function.

class a ~a() public: static a *create() void destory()
Restricted objects can only be built on the stack:

Workaround: Set operator new() to private. Reason: When an object is built on the heap, it is created in a new way, and the underlying layer will call the operator new() function, so as long as the function is restricted, the object can be prevented from being built on the heap.

class a Note that the first argument and return value of the function are fixed: void operator delete(void *ptr) { If you overload new, you need to overload delete public: a() a().

Related Pages