title: Custom smart pointer code issue- couldnt smart pointer end up on heap?
body: <p>Here is some code from a C++ book I have been reading showing how to create your own smart pointer:</p>
  
  <pre><code>template &lt;typename T&gt;
  class SuperSmartPointer
  {
      public:
          explicit SuperSmartPointer(T* inPtr);
          virtual ~SuperSmartPointer();
          SuperSmartPointer(const SuperSmartPointer&lt;T&gt;&amp; src);
          SuperSmartPointer&lt;T&gt;&amp; operator=(const SuperSmartPointer&lt;T&gt;&amp; rhs);
          const T&amp; operator*() const;
          const T* operator-&gt;() const;
          T&amp; operator*();
          T* operator-&gt;();
          operator void*() const { return mPtr; }
      protected:
          T* mPtr;
          static std::map&lt;T*, int&gt; sRefCountMap;
          void finalizePointer();
          void initPointer(T* inPtr);
  };
  
  template &lt;typename T&gt;
  std::map&lt;T*, int&gt; SuperSmartPointer&lt;T&gt;::sRefCountMap;
  
  template &lt;typename T&gt;
  SuperSmartPointer&lt;T&gt;::SuperSmartPointer(T* inPtr)
  {
      initPointer(inPtr);
  }
  
  template &lt;typename T&gt; SuperSmartPointer&lt;T&gt;::SuperSmartPointer(const SuperSmartPointer&lt;T&gt;&amp; src)
  {
      initPointer(src.mPtr);
  }
  
  template &lt;typename T&gt;
  SuperSmartPointer&lt;T&gt;&amp; SuperSmartPointer&lt;T&gt;::operator=(const SuperSmartPointer&lt;T&gt;&amp; rhs)
  {
      if (this == &amp;rhs) {
          return *this;
      }
      finalizePointer();
      initPointer(rhs.mPtr);
      return *this;
  }
  
  template &lt;typename T&gt;
  SuperSmartPointer&lt;T&gt;::~SuperSmartPointer()
  {
      finalizePointer();
  }
  
  template&lt;typename T&gt;
  void SuperSmartPointer&lt;T&gt;::initPointer(T* inPtr)
  {
      mPtr = inPtr;
      if (sRefCountMap.find(mPtr) == sRefCountMap.end()) {
          sRefCountMap[mPtr] = 1;
      } else {
          sRefCountMap[mPtr]++;
      }
  }
  
  template&lt;typename T&gt;
  void SuperSmartPointer&lt;T&gt;::finalizePointer()
  {
      if (sRefCountMap.find(mPtr) == sRefCountMap.end()) {
          throw std::runtime_error(“ERROR: Missing entry in map!”);
      }
      sRefCountMap[mPtr]--;
      if (sRefCountMap[mPtr] == 0) {
          // No more references to this object--delete it and remove from map
          sRefCountMap.erase(mPtr);
          delete mPtr;
          mPtr = nullptr;
      }
  }
  
  template &lt;typename T&gt;
  const T* SuperSmartPointer&lt;T&gt;::operator-&gt;() const
  {
      return mPtr;
  }
  
  template &lt;typename T&gt;
  const T&amp; SuperSmartPointer&lt;T&gt;::operator*() const
  {
      return *mPtr;
  }
  
  template &lt;typename T&gt;
  T* SuperSmartPointer&lt;T&gt;::operator-&gt;()
  {
      return mPtr;
  }
  
  template &lt;typename T&gt;
  T&amp; SuperSmartPointer&lt;T&gt;::operator*()
  {
      return *mPtr;
  }
  </code></pre>
  
  <p>My problem with this code/my question is that surely somebody could instantiate an object of this smart pointer class using the <code>new()</code> operator, have the smart pointer on the heap and therefore what would then manage the memory of the shared pointer?? To make this a proper shared pointer should the <code>new()</code> operator be overloaded/prevented?</p>
  
  <p>EDIT: Shouldnt dynamic memory allocation of the custom smart pointer itself be prevented?</p>