Boost logo

Boost Users :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2006-12-10 18:07:16


----- Mensaje original -----
De: fungos <fungos_at_[hidden]>
Fecha: Domingo, Diciembre 10, 2006 5:38 pm
Asunto: [Boost-users] multi_index + pure abstract
Para: boost-users_at_[hidden]

> Doesn't multi_index accept hold classes that has pure abstract
> methods? Even if these methods aren't used by the container?
> Is there a way to get the container to hold objects with same
> interface?

multi_index_container is in this aspect entirely analogous
to any other container in C++: you can't store elements
of an abstract type because abstract types are, by definition,
not instantiable, but you can store pointers (or smart pointers)
to an abstract type, for instance:

class abstract_base
{
public:
  virtual int f()const=0;
  ...
};

typedef multi_index_container<
  boost::shared_ptr<abstract_base>,
  indexed_by<
    ...
>
> container_type;

As a bonus extra, you can use member functions of abstract_base
(even pure virtual ones) as your key extractors without having
to care about the extra level of dereference imposed by
shared_ptr<>, for instance:

typedef multi_index_container<
  boost::shared_ptr<abstract_base>,
  indexed_by<
    ordered_unique<
      const_mem_fun<abstract_base,int,&abstract_base::f>
>,
    ...
>
> container_type;

This "automatic dereferencing" capability of B.MI predefined
key extractors is explained at http://tinyurl.com/mpjae .

Hope this helps,

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net