title: std::shared_ptr and Inheritance
body: <p>I am having some problems with automatic typecasting between <code>shared_ptr</code> of inherited classes.</p>
  
  <p>My class structure is as follows, a base class <code>Base</code> and two derived classes <code>Derived1</code> and <code>Derived2</code>.</p>
  
  <pre><code>// Base class
  class Base {
  protected:
    ...
    ...
  public:
    Base() = default;
    virtual ~Base() = default;
    virtual void run() = 0;
    ...
    ...
  };
  
  // Derived class
  class Derived1: Base {
  protected:
    ...
    ...
  public:
    Derived1() = default;
    virtual ~Derived1() = default;
    void run() {...}
    ...
    ...
  };
  
  // Derived class
  class Derived2: Base {
  protected:
    ...
    ...
  public:
    Derived2() = default;
    virtual ~Derived2() = default;
    void run() {...}
    ...
    ...
  };
  </code></pre>
  
  <p>I have a function <code>doSomething()</code></p>
  
  <pre><code>void doSomething(std::shared_ptr&lt;Base&gt; ptr) {
    ptr-&gt;run();
    ...
  }
  </code></pre>
  
  <p>I call the function with the derived classes like so -</p>
  
  <pre><code>doSomething(make_shared&lt;Derived1&gt;())
  doSomething(make_shared&lt;Derived2&gt;())
  </code></pre>
  
  <p>But I get an error saying -</p>
  
  <pre><code>no viable conversion from 'shared_ptr&lt;class Derived1&gt;' to 'shared_ptr&lt;class Base&gt;'
  no viable conversion from 'shared_ptr&lt;class Derived1&gt;' to 'shared_ptr&lt;class Base&gt;'
  </code></pre>
  
  <p>What am I doing wrong? Is it safe just to use <code>static_pointer_cast</code> to the Base type? Like -</p>
  
  <pre><code>doSomething(static_pointer_cast&lt;Base&gt;(make_sahred&lt;Derived2&gt;()))
  </code></pre>
  
  <p><em><strong>SOLUTION</strong></em>
  My bad... The problem was that I was inheriting the base class privately.</p>