std::map unable to handle polymorphism?

前端 未结 5 596
时光说笑
时光说笑 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条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 00:06

    No, you must store a pointer to the base class to get the polymorphical behavior. The way you do now will lead to slicing.

提交回复
热议问题