Update syntaxhighlight tags - remove use of deprecated <source> tags

← Older revision Revision as of 06:52, 16 April 2020
Line 13: Line 13:
 
In this case, programmers must use <code>new</code> to create the objects and prohibit stack-based and objects of static duration. The idea is to prevent access to one of the functions that are always needed for stack-based objects: constructors or destructors. Preventing access to constructors, i.e., protected/private constructors will prevent heap-based objects too. Therefore, the only available way is to create a protected destructor as below. Note that a protected destructor will also prevent global and namespace-scope objects because eventually the objects are destroyed but the destructor is inaccessible. The same is applicable to temporary objects because destroying temporary objects need a public destructor.
 
In this case, programmers must use <code>new</code> to create the objects and prohibit stack-based and objects of static duration. The idea is to prevent access to one of the functions that are always needed for stack-based objects: constructors or destructors. Preventing access to constructors, i.e., protected/private constructors will prevent heap-based objects too. Therefore, the only available way is to create a protected destructor as below. Note that a protected destructor will also prevent global and namespace-scope objects because eventually the objects are destroyed but the destructor is inaccessible. The same is applicable to temporary objects because destroying temporary objects need a public destructor.
   
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
class HeapOnly {
 
class HeapOnly {
 
public:
 
public:
Line 30: Line 30:
 
HeapOnly h2; // Destructor is protected so h2 can't be created on stack
 
HeapOnly h2; // Destructor is protected so h2 can't be created on stack
 
}
 
}
  +
</syntaxhighlight>
</source>
 
   
 
Protected destructor also prevents access to <code>delete HeapOnly</code> because it internally invokes the destructor. To prevent memory leak, <code>destroy</code> member function is provided, which calls <code>delete</code> on itself. Derived classes have access to the protected destructor so <code>HeapOnly</code> class can still be used as a base class. However, the derived class no longer has the same restrictions.
 
Protected destructor also prevents access to <code>delete HeapOnly</code> because it internally invokes the destructor. To prevent memory leak, <code>destroy</code> member function is provided, which calls <code>delete</code> on itself. Derived classes have access to the protected destructor so <code>HeapOnly</code> class can still be used as a base class. However, the derived class no longer has the same restrictions.
Line 39: Line 39:
   
   
<source lang="cpp">
+
<syntaxhighlight lang="cpp">
 
class NoHeap {
 
class NoHeap {
 
protected:
 
protected:
Line 52: Line 52:
 
new NoHeapTwo[10]; // Not allowed because of inherited protected new operator (#2).
 
new NoHeapTwo[10]; // Not allowed because of inherited protected new operator (#2).
 
}
 
}
  +
</syntaxhighlight>
</source>
 
 
The above declaration of protected <code>new</code> operator prevents remaining compiler-generated versions, such as placement new and nothrow new. Declaring just the scalar <code>new</code> as protected is not sufficient because it still leaves the possibility of <code>new NoHeap[1]</code> open. Protected <code>new []</code> operator prevents dynamic allocation of arrays of all sizes including size one.
 
The above declaration of protected <code>new</code> operator prevents remaining compiler-generated versions, such as placement new and nothrow new. Declaring just the scalar <code>new</code> as protected is not sufficient because it still leaves the possibility of <code>new NoHeap[1]</code> open. Protected <code>new []</code> operator prevents dynamic allocation of arrays of all sizes including size one.