std::map unable to handle polymorphism?

前端 未结 5 598
时光说笑
时光说笑 2021-01-18 23:38

When using std::map in c++, is it possible to store inherited classes as their \"base class\" in the map and still being able to call their overloaded methods? See this exam

5条回答
  •  -上瘾入骨i
    2021-01-19 00:04

    Dynamic polymorphism works only with references and pointers. In case of using containers, which copy objects around inside them -- only with pointers.

    If you would force a cast from super type to base type without using pointer or reference, the object memory would be sliced. Check this out: Object slicing wikipedia article

提交回复
热议问题