Polymorphic objects on the stack?

In a previous question I quoted Stroustrup on why a common Object class for all classes is problematic in c++. In that quote there is the statement:

Using a universal base class implies Стоимость: объекты должны быть размещены в куче to be polymorphic;

I really didn't look twice at it, and since its on Bjarnes home page I would suppose a lot of eyes have scanned that sentence and reported any misstatements.

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 "VDerived::f()".

struct VBase
{
    virtual void f()    { cout<<"VBase::f()"<

Of course if the formal argument to test was test(VBase obj) the case would be totally different, but that would not be a stack vs. heap argument but rather copy semantics.

Is Bjarne flat out wrong or am I missing something here?

30
задан Community 23 May 2017 в 11:54
поделиться