Polymorphic objects on the stack?

后端 未结 7 677
忘了有多久
忘了有多久 2021-02-01 02:53

In Why is there no base class in C++?, I quoted Stroustrup on why a common Object class for all classes is problematic in c++. In that quote there is the statement:

7条回答
  •  清歌不尽
    2021-02-01 03:37

    I think he was going along the lines of not being able to store it in a base-typed variable. You're right in saying that you can store it on the stack if it's of the derived type because there's nothing special about that; conceptually, it's just storing the data of the class and it's derivatives + a vtable.

    edit: Okay, now I'm confused, re-looking at the example. It looks like you may be right now...

提交回复
热议问题