c++ - Saving an object of a derivated class in a std::list of an base c -
i'm trying save object of x class, derivated base class y in std::list<y>
. x class has stuff y class doesn't has, , when save object std::list
, object slicing happens. there more, different classes use y base, simplicity, using 1 storage ideal, thing have different members, using static_cast
didn't work expected. there workaround problem? or should give , use different containers each type?
keep list of y*
. have full virtual function resolution @ disposal and, if needed, can dynamic_cast
(although advise against that).
the reason latter being there reason why keep list-of-y's in stead of else.