Before starting, analyze the association between the new operator and operator new in C ++.
New: A common operator in C ++, such as a * A = new A or a constructor with parameters. For new, there are new and :: the former is in STD.
Operator new (): indicates an overloaded form of the new operator. It is a function, not an operator. Operator new can be divided into global overload and class overload. The Global overload is void *: Operator new (size_t size), and void * :: operator new (size_t size ). Note thatOperator new () generally only allocates memory.In fact, the system's default Global: Operator new (size_t size) only calls malloc to allocate memory and returns a void * pointer. WhileThe call of the constructor (if needed) is completed in the new operator..
First, briefly explain the relationship between new and operator New: for the relationship between the two, I found a classic description (from www.cplusplus.com, see references ):
operator new
Can be called explicitly as a regular function, but in C ++,
new
Is an operator with a very specific behavior: an expression with
new
Operator, first CILS Function
operator new
(I. E ., this function) with the size of its type specifier as first argument, and if this is successful, it then automatically initializes or constructs the object (if needed ). finally, the expression evaluates as a pointer to the appropriate type. for example, we can write the following code: A * A = new A; we know that there are two steps: 1. allocate memory, 2. call a () to construct the object. In fact, the memory allocation operation is completed by operator new (size_t). If Class A reloads operator new, A: Operator new (size_t) is called ), if there is no overload, call: Operator new (size_t). The Global New operator is provided by C ++ by default. So the previous two steps are: 1. Call operator new 2. Call the constructor.
Generally, you can write a class to allocate space on the stack or stack. However, sometimes you want to write a class that can only allocate space on stacks or stacks. Can this be implemented? Think about it.
In C ++, Class Object creation can be divided into two types: static creation, such as a A and dynamic creation, such as a * PTR = new; the two methods are different.
1. Static creation of class objects: the compiler allocates memory for the objects in the stack space, and moves the top pointer of the stack to the appropriate space, then, the constructor is called in the memory space to form a stack object. This method is used to directly call the class constructor.
2. Create a class object dynamically by using the new operator to create the object in the heap space. This process is divided into two steps. The first step is to execute the operator new () function, search for the appropriate memory in the heap space and allocate it. The second step is to call the constructor to construct the object, initialize the memory space. This method indirectly calls the constructor of the class.
1. You can only allocate class objects on the stack. You cannot create class objects statically, that is, you cannot directly call class constructors.
It is easy to think of setting the constructor as private. After the constructor is private, you cannot call constructor outside the class to construct class objects. It seems that you can only use the new operator to create objects. However, as mentioned earlier, the execution process of the new operator is divided into two steps. c ++ provides the overload of the new operator, in fact, only the operator new () function is allowed to be reloaded, while operator new () the function is only used to allocate memory and cannot provide the constructor function. You must call the constructor outside the class. Therefore, this method is not acceptable.
When an object is built on the stack, the compiler allocates the memory space and calls the constructor to construct the stack object. After an object is used up, the compiler calls the destructor to release the space occupied by the stack object. The compiler manages the entire lifecycle of an object. What will happen if the compiler cannot call the class destructor? For example, the class destructor are private and the compiler cannot call the destructor to release the memory. So,
When the compiler allocates stack space for class objects, it first checks the access of class destructor. In fact, it is not only a destructor, but also a non-static function. If the class destructor are private, the compiler will not allocate memory for the class object in the stack space.Therefore,
If you set the destructor to private, the class object cannot be created on the stack.. The Code is as follows:
class A { public: A(){} void destory(){delete this;} private: ~A(){} };
Try to use a A; to create an object and report an error during compilation, prompting that the Destructor cannot be accessed. In this way, only the new operator can be used to create objects. constructors are public and can be called directly. Class must provide a destory function to release the memory space. After the class object is used, you must call the destory function. Without this function, the object created using the new operator cannot be released because it cannot use the destructor.
Disadvantages of the above method:
I. Inheritance issues cannot be solved. If a is the base class of other classes, The Destructor is usually set to virtual and then overwritten in the subclass to realize polymorphism. Therefore, the Destructor cannot be set to private. Fortunately, C ++ provides the third type of access control, protected. Setting the Destructor as protected can effectively solve this problem. If you cannot access the protected member outside the class, you can access the subclass.
II. It is inconvenient to use classes. Instead of using Delete, you can use the destory function to release objects by using new to create objects.(If you use Delete, an error is returned because the pointer to the delete object calls the destructor of the object, which is not accessible outside the Destructor class.
For unification, you cannot call the delete destructor or call New to create objects. You can set the constructor to protected, and then provide a public static function to construct the constructor. Instead of using new, you can use a function to construct the constructor and use a function to analyze the constructor. The Code is as follows, similarSingleton Mode:
class A { protected: A(){} ~A(){} public: static A* create() { return new A(); } void destory() { delete this; } };
In this way, call the CREATE () function to create Class A on the stack and call the destory () function to release the memory.
2. class objects can only be allocated on the stack
Objects are created on the stack only when the new operator is used. Therefore,If the new operator is disabled, class objects can only be created on the stack. Although you cannot influence the new operator (because it is built in C ++), you can use the fact that the new operator always calls operator new first, the latter can be declared and rewritten by ourselves. Therefore,Set operator new () to private to prevent objects from being added to the heap. Note that the new operator cannot be used, which means that delete cannot be used. The Code is as follows:
Class A {PRIVATE: void * operator new (size_t t) {}// note that the first parameter and return value of the function are fixed void operator Delete (void * PTR) {} // if new is reloaded, the delete public: A () {} ~ must be reloaded (){}~ A (){}};
How to allocate space to objects only in the heap or stack