Boost logo

Boost :

From: Daryle Walker (darylew_at_[hidden])
Date: 2001-10-22 13:16:39


I remember occasionally seeing posts about storing objects of the same
(abstract?) base type, but of different concrete types, in a container.
This can't be directly done with the standard containers, since all the
elements have to have the same type structure. The closest we can get is to
use a container of base_type*, dynamically allocate the objects, and
remember to take care of elements' removal when done.

We have an "any" type that can hold any type of object (hence the name). I
wonder if we can use this to make a container that seems to hold various
objects derived from a base type. It would basically be a
std::vector<boost::any> (or a deque) that has its element-adding procedure
filter out any objects that aren't of the given base type (presumably a
type-based template parameter).

Assuming that "base_type" is polymorphic by having at least one virtual
function, then we can filter by using that base type in the insert member
functions. There's a complication: if we have a

boost::poly_deque<base_type>::push_back( base_type const &x );

member function to add a new element, we have to make sure that the final
type of "x" is determined before copying it to the internal "any" object
used for that element. Otherwise, slicing may occur.

Thoughts?

-- 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk