|
Boost : |
From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-01-18 13:33:35
"Pavel Vozenilek" <pavel_vozenilek_at_[hidden]> wrote in message
news:bu91r1$bo0$1_at_sea.gmane.org...
> Problem:
> ----------
> Often one would like to have some data value associated with a
container.
>
> E.g. with list of window controls one may find handy to have
pointer to
> parent
> window "be somewhere within" the list.
>
>
> Current Solutions:
> --------------------
> a) Use std::pair<list<WindowControl>, Window*> pair. But it makes
code
> accessing
> the list more clumsy (add .first everywhere). It is pain if
needed in
> the middle of
> a project, even more if the need for associated pointer
disappears
> (and reappears again).
>
Why not:
struct WindowList : list<WindowControl*> : noncopyable {
WindowList() : parent(0) { }
WindowControl* parent;
};
?
I know it violates the dictum 'never derive from a concrete class',
but it seems fairly harmless. Of course, you might have to define your
own constructors. And if you dispense with noncopyable you have to
write a copy-assignment operator.
>
> Sample Implementation:
> ---------------------------
> Changing container class is relatively easy. I attached container
MyArray
> (array of 2 items) that can use associated data as example (tested
on
> Intel C++ 7.0).
Could you post it at Yahoo? My virus software ate your attachment (and
the partially digested version is hard to read :-)
Jonathan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk