Overriding parent class's function [duplicate]

ぐ巨炮叔叔 提交于 2019-12-01 22:01:25

This happens because of object slicing, you'll need to keep a vector of pointers (preferably smart pointers).

I'm assuming stuff is defined as std::vector<classa> stuff;. When you do

stuff.push_back(b);

the object pushed into the vector is a slice of b - particulary the classa part. All other type info is lost. For this to work as expected, you'd need:

std::vector<classa*> stuff;

or similar. The way your code is now, you can't get it to work because stuff[1] is no longer a classb, but a classa.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!