Boost logo

Boost :

From: JOAQUIN LOPEZ MU?Z (joaquin_at_[hidden])
Date: 2004-01-18 18:02:33


----- Mensaje original -----
De: Pavel Vozenilek <pavel_vozenilek_at_[hidden]>
Fecha: Domingo, Enero 18, 2004 10:59 pm
Asunto: [boost] Re: [idea] arbitrary data associated with container

>
> "Jonathan Turkanis" <technews_at_[hidden]> wrote
>
> > > Often one would like to have some data value associated with a
> > > container.
> > >
> > Why not:
> >
> > struct WindowList : list<WindowControl*> : noncopyable {
> > WindowList() : parent(0) { }
> > WindowControl* parent;
> > };
> > ?
> >
> Well, it adds one layer to object hierarchy. I personally find this
> as unnecessary complicating design.
> Associated data should not be so intrusive.
>
> Woudn't you like more an "attachment policy"?
>

Well, I endorse Jonathan approach of deriving from a
non-attachment class. If I understand it clearly, your
solution cannot be applied to preexisting STL containers
(am I right?)

>
> Other point is that you may too easily convert it to list<...> pointer
> (and loose associated data in the process)
> and you may easily delete the list<...> pointer.
>

I'd say that WindowList IS-A std::list, so passing it to
a function excepting an std::list<WindowControl *> is
all right. The inadvertent delete is certainly an issue,
but no more that in other situations where no IS-A
relationship is involved:

void foo(SomeClass *x)
{
  delete x;
}

foo(new SomeClass()); //ok

SomeClass x;
foo(x); // ko

IMHO, deletion needs extra considerations apart from
the issue of non-virtual derivation, so it is not so
bad to apply derivation here. Main benefit is you
can use this idiom with preexisting classes.

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


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