title: Destructor for class that holds pointer to object
body: <p>Lets say we have a class that holds a pointer member to another object. If I delete that pointer in the destructor I get an error (and I understand why). </p>
  
  <p>My question is : is it possible to overcome that without memory leaks ? </p>
  
  <p>Here is a demo of what I am doing. </p>
  
  <pre><code>class A {
      public:
      ~A() { cout&lt;&lt; "~A()" &lt;&lt;endl; }
  };
  
  class B {
      A *pA;
      public:
      B(A* pA) {
          this-&gt;pA = pA;
      }
      ~B() { 
                  delete pA;
          cout&lt;&lt;"~B()"&lt;&lt;endl;
      }
  };
  
  int main() {
      A a;
      {
          B b2(new A()); //deletes A, deletes B, no memory leaks
      }
      {
          B b(&amp;a); //deletes A, error.
      }
  
      return 0;
  }
  </code></pre>