title: What would cause a C++ object's type information to change after it is returned from a function?
body: <h1>Introduction</h1>
  <p>I have C++17 code of the following form:</p>
  <pre><code>Base&amp; makeDerived(int val) {
    std::shared_ptr&lt;Derived&gt; derived = secondLayer(val);
    std::cout &lt;&lt; typeid(derived).name() &lt;&lt; std::endl;
    return *derived;
  }
  
  int main(void) {
      Base&amp; derived = makeDerived(7);
      std::cout &lt;&lt; typeid(derived).name() &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>
  <p>I can get this code to either print:</p>
  <pre><code>Derived
  Derived
  </code></pre>
  <p>or</p>
  <pre><code>Derived
  Base
  </code></pre>
  <p>with different implementations of Derived and Base, but I don't understand why this would happen.</p>
  <h2>Supplemental Notes</h2>
  <p>The actual code I am basing this question on prints: <code>Derived, Base</code> but it would be much too long to paste, and I wouldn't be authorized to do so. I can't figure out how to distill it down to a characteristic example, all efforts seem to print <code>Derived, Derived</code>, and the fact that I can't distill it down is basically what my problem is because I don't understand why it would be different.</p>
  <p>In all implementations I care about, <code>Derived</code> is a descendant of <code>Base</code>.</p>
  <h2>Question</h2>
  <p><em>What would cause a descendant of a base class to change its type information to the base class itself after exiting a function?</em></p>