Quick Q: How do you have an STL container of polymorphic objects? -- StackOverflow

Quick A: Try vector<unique_ptr<base>> by default.

What's the preferred C++ idiom to own a collection of polymorphic objects?

... I'm sure there's something related to move semantics in there (I could move the objects passed to add_items() to own them) but I still can't figure out how my collection would be declared.

What is the C++ idiom for this sort of polymorphic ownership (particularly with STL containers)?

Add a Comment

Comments are closed.

Comments (1)

0 0

matt said on Jul 10, 2013 07:48 PM:

Why not simply std::vector<std::reference_wrapper<base>>?