T
Tim H
I'm wondering if there is a way to do this properly. I feel like I am
missing something.
I have two distinct classes. They don't have much at all in common.
I want a vector-like container that contains smart pointers
(boost::shared_ptr or similar) to a set of these two classes and keeps
them sorted wrt each other.
For example if I insert ClassA, ClassA, ClassB, ClassA, ClassB in that
order, I want to re-visit them in that same order when I iterate
later.
My first thought was to make an artificial base class that has a
'type' argument indicating which "derived" type was stored. Then a
simple vector< shared_ptr<Base> > gets me my objects back. The
problem, of course, is that I get back pointers to the Base type.
Is there an idiomatic way to go from shared_ptr<Base> to
shared_ptr<Derived> ? I note that the compiler happily goes the other
way automatically.
Or maybe there is another idiomatic way to store this data? I can
think of a half-dozen ways (seperate vectors with a third switcher
vector, etc), but they all seem dirty.
Is there a better way?
Tim
missing something.
I have two distinct classes. They don't have much at all in common.
I want a vector-like container that contains smart pointers
(boost::shared_ptr or similar) to a set of these two classes and keeps
them sorted wrt each other.
For example if I insert ClassA, ClassA, ClassB, ClassA, ClassB in that
order, I want to re-visit them in that same order when I iterate
later.
My first thought was to make an artificial base class that has a
'type' argument indicating which "derived" type was stored. Then a
simple vector< shared_ptr<Base> > gets me my objects back. The
problem, of course, is that I get back pointers to the Base type.
Is there an idiomatic way to go from shared_ptr<Base> to
shared_ptr<Derived> ? I note that the compiler happily goes the other
way automatically.
Or maybe there is another idiomatic way to store this data? I can
think of a half-dozen ways (seperate vectors with a third switcher
vector, etc), but they all seem dirty.
Is there a better way?
Tim