Item 34. Restrick Heap Allocation

xiaoxiao2021-03-05  27

Item 34. Restrick Heap Allocation

1. What circumstances should it limit the allocation? If a handle object ("" Handle "Object) maintains a number of items (" Body "Object) quantity, you need to ensure that the object's destructor is called. Local objects and static objects can automatically call their destructor, and the objects allocated by the heap need to be explicitly modified. This ensures that this special class object cannot be assigned to be a good notice.

2, how to limit the heap allocation? Class noHeap {public: // ... protected: void * operator new (size_t) {return 0;} void Operator delete (void *) {} private: void * operator new [] (size_t); void operator delete [] (void *);

3, how to limit stack assignments?

Class onheap {~ onheap (); public: void destroy () {delete this;} // ...};

转载请注明原文地址:https://www.9cbs.com/read-35211.html

New Post(0)