title: C++ class destructor delete member if "owner"?
body: <p>I know in C++ that a pointer is just that: a pointer to a memory location, and there is no concept of "owners". But consider the following situation (not necessarily good code):</p>
  
  <pre><code>class A {
  public:
      A(){}
      ~A()
      { if(myObject!=nullptr)
          delete myObject;
      }
  
      void createMember()
      {myObject=new CrazyCustomClass();}
  
      CrazyCustomClass *getMember()
      {return myObject;}
  private:
      CrazyCustomClass *myObject=nullptr;
  }
  </code></pre>
  
  <p>If it makes a difference, CrazyCustomClass does NOT have a copy constructor, as it makes no sense to copy it. So pretty straight forward - I have a class that, at some point after instantiation, may call <code>new</code> to instantiate a member of type <code>CrazyCustomClass *</code></p>
  
  <p>The problem is that if at some point I have a copy of <code>class A</code> created (which is fine - I want to be able to copy <code>class A</code>). When that <em>copy</em> is deleted, so is the object pointed to by the <em>original</em> class A instantiation. For example:</p>
  
  <pre><code>void StupidFunction(A *firstObject){
  //This is NOT a real function, it simply illustrates the effect of a third-party library function
      //create a new object that is a copy of first object
      A secondObject(*firstObject);
      &lt;do whatever with second object&gt;
      //secondObject goes out of scope here and gets deleted.
  }
  
  A *firstObject=new A();
  firstObject-&gt;createMember();
  stupidFunction(firstObject);
  CrazyCustomClass *customObject=firstObject.getMember(); //this is now an invalid pointer
  </code></pre>
  
  <p>In the above example, the <code>StupidFunction</code> is from a third-party library, the idea being that it gives a "temporary" copy of the object that you can work with without messing with the original object, which is good. <code>Class A</code> and <code>CrazyCustomClass</code> are both my code and can be changed at will. Unfortunately, when the "temporary" copy is deleted, the way I wrote my destructor causes problems. </p>
  
  <p>My first thought was to use shared_ptr, something like so:</p>
  
  <pre><code>std::shared_ptr&lt;CrazyCustomClass&gt; sharedObject=std::make_shared&lt;CrazyCustomClass&gt;(new CrazyCustomClass);
  </code></pre>
  
  <p>...but that gave me an error when compiling:</p>
  
  <blockquote>
    <p>candidate constructor (the implicit copy constructor) not viable: no
    known conversion from 'CrazyCustomClass *' to 'const CrazyCustomClass'
    for 1st argument; dereference the argument with *</p>
  </blockquote>
  
  <p>and if I <em>do</em> dereference the argument with *, it gives me an error about the copy constructor of <code>"CrazyCustomClass"</code> being deleted, which is true - there is no sensible way to copy <code>CrazyCustomClass</code>.</p>
  
  <p>So my question is: how can I refactor <code>class A</code> such that <code>myObject</code> gets properly deleted when <code>firstObject</code> goes out of scope, but <em>not</em> when any "temporary" copies of A get deleted?</p>