class - C++ call a child's method from a vector of parents? -
say have following class:
class parent { // methods , members go here };
then create child based on parent:
class child : public parent { public: somefunction(); };
now class parent doesn't have somefunction() class child does. have vector of parent classes, , want call child's method, assuming vector holds instances of parents , children, given they're both same base type.
std::vector<parent> mycontainer; mycontainer.push_back(a parent); mycontainer.push_back(a child); mycontainer[1].somefunction();
how can make work? i'm trying create vector of parents, children go in vector. want call method that's exclusive children. possible?
you create vector of parent
objects. there's no polymorphism here. vectors contain values. if want polymorphic container, have use else.
it's if did
parent myobject;
no matter myobject
later, still parent
. if myobject = child()
.
Comments
Post a Comment