title: C++ Pointer data members: Who should delete them?
body: <p>So say I have a class like this:</p>
  
  <pre><code>class A {
      public:
          A( SomeHugeClass* huge_object)
              : m_huge_object(huge_object) {}
      private:
          SomeHugeClass* m_huge_object;        
  };
  </code></pre>
  
  <p>If someone uses the constructor like this:</p>
  
  <pre><code>A* foo = new A(new SomeHugeClass());
  </code></pre>
  
  <p>Who's responsibility is it to call delete on the object newed in the constructor? In this case, the scope in which the A constructor was called can only delete foo since the SomeHugeClass is anonymous.</p>
  
  <p>However, what if someone uses the constructor like this?</p>
  
  <pre><code>SomeHugeClass* hugeObj = new SomeHugeClass();
  A* foo = new A(hugeObj);
  </code></pre>
  
  <p>Then, the caller can call delete hugeObj at some point, right?</p>
  
  <p>Does this implementation of A leak memory on destruction?</p>
  
  <p>I'm working on a project with a lot of object composition done this way and as much as I would love to use smart pointers, I have to talk to the project leads about changing old code to take advantage of that before I can.</p>