Why can’t we declare a std::vector?

You can’t instantiate abstract classes, thus a vector of abstract classes can’t work.

You can however use a vector of pointers to abstract classes:

std::vector<IFunnyInterface*> ifVec;

This also allows you to actually use polymorphic behaviour – even if the class wasn’t abstract, storing by value would lead to the problem of object slicing.

Leave a Comment