You haven't shown us the use of Nodes
and Top
, Middle
and
Bottom
, but from the looks of things, I don't think this can
be made to work, for several reasons.
The most important reason is simple: a std::vector<Node>
can
only contain objects of type Node
. Objects of type Top
,
Middle
and Bottom
will be sliced (converted to Node
) if
inserted, so you loose the derived type.
And you cannot do something like struct Top : public
std::vector<Top>
(not that I think that's what you want), since
you cannot instantiate a vector over an incomplete type, and you
cannot derive from an uninstantiated template.
The usual solution for this is to create a Tree
class, and use
std::vector<Node*>
in the nodes (as a member, not via
inheritance). Alternatively, you can use Boost's pointer vector
(but be careful of lifetime of the objects), or if you know that
you'll never need back pointers, you can use
std::vector<std::unique_ptr<Node>>
(but the interface becomes
a little trickier, and you still need to be careful about the
lifetime of objects—removing an object from the tree will
destroy it).