mardi 18 juillet 2017

C++11 vector holding 2 different subclasses, but not at the same time

Suppose I have a base class Base, two subclasses A and B, with the latter having itself a subclass C.

I would like to enforce by design that an std::vector can hold objects of either types (subclasses like C included), but only one type at a time -- that is, either I have a vector of As, or a vector of Bs and Cs.

Is it possible to have this in C++11 (of course, I could enforce this constraint when adding elements to the vector, but I wonder if I could prevent this via a clever design)? How can I structure the class hierarchy to obtain this?

Thanks in advance for your help!

R.

Aucun commentaire:

Enregistrer un commentaire