How to design a C ++ class can only create objects on the heap or stack

Design a class that can only be created on the heap objects

  • The private class constructor, declared as a private copy constructor. Prevent others call the copy to generate an object on the stack.
  • Providing a static member function, complete the creation of heap objects in the static member function

note

  1. We will call the constructor to create objects on the heap and stack in order to prevent the creation of an object on the stack we will constructors privatization.
  2. Copy constructor is to create objects on the stack.
. 1  class HeapOnly
 2  {
 . 3  public :
 . 4      static HeapOnly * the CreateObject ()
 . 5      {
 . 6          return  new new HeapOnly; // This makes the creation of such objects only through new, can insure that the class object is created in the heap 
7      }
 8      
. 9  Private :
 10      HeapOnly () {} // create the object will be called in the constructor of the heap and stack, in order to prevent the creation of an object on the stack constructor will be privatized 
. 11      HeapOnly ( const HeapOnly &) = Delete ; // copy constructor object is created on the stack 
12 };

 

Design a class that can only create objects on the stack

method one:

  • The class constructor private. Prevent objects created on the heap
  • Providing a static member function, create a stack object to complete the static member function

note

  1. Created on the heap and stack objects will call the constructor, in order to prevent objects created on the heap, should be privatized constructor.
. 1  class StackOnly
 2  {
 . 3  public :
 . 4      static StackOnly the CreateObject ()
 . 5      {
 . 6          return StackOnly (); // insure that the class is not the new operator to create objects so that such objects can only be created on the stack 
7      }
 . 8  Private :
 . 9      StackOnly () {} // create the object will be called in the constructor of the heap and stack, in order to prevent objects created in the heap, the constructor should be privatized 
10 };

 

Method Two objects can only be created on the stack, that can not be created on the heap, so as long as the new functions can be masked, that shield the operator new and placement new expressions Note: shielding the operator new, practical will also be located new masked.

1 class StackOnly    
2 {    
3 public:        
4     StackOnly()  {} 
5 private:        
6     void* operator new(size_t size);    
7     void operator delete(void* p); 
8 };  

 

note

  • new and delete user for dynamic memory allocation and release operators, operator new and operator delete global functions provided by the system, new to apply space of the calling operator new global function at the bottom, delete the underlying free up space by operator delete global function .
  • operator new function malloc to apply through the actual space, application space when malloc successful return directly; application space fails, try to execute insufficient space response measures, countermeasures if the change user settings, and then continue to apply, or throw an exception.
  • operator delete function is ultimately to free up space by free

Guess you like

Origin www.cnblogs.com/al-fajr/p/11846269.html