On 12/20/2011 4:47 PM, Jayden Shui wrote:
> I have a number of named objects of type B whose name can be accessed
> by B::Name(). B is polymorphic. D1, D2 and so on are derived from B. I
> want to put them in a set or map-like container ordered by their
> names. They can be used by other objects, such as of type C. If the
> objects in the container are changed by its type (from type D1 to D2)
Huh? Type is a lifelong attribute of any object. Objects do NOT change
their types. What you could have is an object of type D1 goes away and
another object (of type D2) is put it the former object's place.
> or value, their users can know it automatically, i.e. those
> information is shared among owners and users. To erase an object in
> the container, first we need to ensure it is not used by other
> objects.
>
> Please give me your idea on it by choosing a proper container with
> smart pointers. I am struggling with it and cannot find a good way.
Containers aren't chosen by the behavior of their contained elements.
They are usually chosen by the speed/complexity of access, memory
requirements for storage, const-ness of objects, etc. Associative
containers can either be automatically sorted (you can have a set of
objects sorted by means of any custom comparator), etc. What you need
is probably a multiset.
V
--
I do not respond to top-posted replies, please don't ask