title: Inheritance and smart pointers (std::shared_ptr)
body: <p>There are a lot of things that needs to be said. First off, I would like to know if the approach below is considered a design pattern or even a common technique (That's why I didnt provide further information on the title). If thats the case, whats the name?
  Anyways, this is a minified version of what I'm trying to achieve. Since I need to use copying, I found that using std::shared_ptr is the best to avoid deallocated (deleted) pointers.</p>
  
  <pre><code>class Foo
  {
  public:
      Foo() : ptr(nullptr) {}
      Foo(const Foo&amp; foo) : ptr(foo.ptr) {}
      virtual ~Foo() = default;
  
      void whatever() {
          if (ptr)
              ptr-&gt;whateverHandler();
      }
  
      void reset() {
          ptr.reset();
      }
  
      void resetBar() {
          ptr.reset(new Bar);
      }
  
      // Other resets here...
  
  protected:
      Foo(Foo* foo) : ptr(foo) {}
  
  private:
      // Every child class should override this
      virtual void whateverHandler() {
          throw "whateverHandler cant be called within base class";
      }
  
  protected:
      std::shared_ptr&lt;Foo&gt; ptr;
  };
  
  class Bar : public Foo
  {
  public:
      Bar() : Foo(this) {}
      void whateverHandler() {
          printf("Bar's handler!!! \n");
      }
  };
  </code></pre>
  
  <p>This all looks good and compiles fine, however, the following exame crashes. Why is that?</p>
  
  <pre><code>int main()
  {
      {
          Foo f;
  
          f.resetBar();
      }
  
      return getchar();
  }
  </code></pre>