mercredi 7 novembre 2018

vector of X : public Z without pointers or inheritance

struct X { virtual void go() = 0; };
struct Y : public X { void go() override {} };
struct Z : public X { void go() override {} };

some usage such as:

vector<shared_ptr<X>> v = { make_shared<Y>(), make_shared<Z>() };
for (auto& c : v ) { c->go() };

is there a better (but not too complex) way to do this without heap allocations, ptrs and inheritance?

Aucun commentaire:

Enregistrer un commentaire