title: Polymorphic objects on the stack?
body: <p>In <a href="https://stackoverflow.com/questions/5697328/why-is-there-no-base-class-in-c">Why is there no base class in C++?</a>, <a href="/a/5697373">I quoted Stroustrup</a> on why a common Object class for all classes is problematic in c++. In that quote there is the statement:</p>
  <blockquote>
  <p>Using a universal base class implies cost: Objects must be heap-allocated to be polymorphic;</p>
  </blockquote>
  <p>I really didn't look twice at it, and since its <a href="//www.stroustrup.com/bs_faq2.html#object" rel="nofollow noreferrer">on Bjarnes home page</a> I would suppose a lot of eyes have scanned that sentence and reported any misstatements.</p>
  <p>A commenter however pointed out that this is probably not the case, and in retrospect I can't find any good reason why this should be true. A short test case yields the expected result of <code>VDerived::f()</code>.</p>
  <pre><code>struct VBase {
      virtual void f() { std::cout &lt;&lt;&quot;VBase::f()\n&quot;; }
  };
  
  struct VDerived: VBase {
      void f() { std::cout &lt;&lt; &quot;VDerived::f()\n&quot;; }
  };
  
  void test(VBase&amp; obj) {
      obj.f();
  }
  
  int main() {
      VDerived obj;
      test(obj);
  }
  </code></pre>
  <p>Of course if the formal argument to test was <code>test(VBase obj)</code> the case would be totally different, but that would not be a stack vs. heap argument but rather copy semantics.</p>
  <p>Is Bjarne flat out wrong or am I missing something here?</p>
  <p>Addendum:
  I should point out that Bjarne has added to the original FAQ that</p>
  <blockquote>
  <p>Yes. I have simplified the arguments; this is an FAQ, not an academic paper.</p>
  </blockquote>
  <p>I understand and sympathize with Bjarnes point. Also I suppose my eyes was one of the pairs scanning that sentence.</p>