On Jun 11, 7:31 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Max Odendahl wrote:
my own declared class has a stl::list as a member variable. I now need
access to those from the outside. Is a custom iterator for my class
the best solution for this and how to do this?
No. You can expose the entire list by returning it by reference from
some kind of accessor function. So, it all depends on how you define
"best".
Generally, I've found this not to be a very good idea. You
decide the minimum that you have to guarantee, and wrap the
iterator of whatever container you're using to only support
that. (For example, you might decide only to "expose" a forward
iterator, in order to keep your options open, even though the
current implementation uses std::vector.)
I would like to sort the list according to different criteria before,
so I would like to offer different iterators, which then sort first
based on the type of iterator constructed.
Iterators don't sort, they aren't supposed to. Iterators iterate.
Over a sequence. And a sequence has order. Iterators thus
expose order, and at least conceptually, could induce it.
In practice, of course, I'm not to sure how you'd go about
providing an iterator which exposes an order not supported by
the underlying container. (It depends, of course. I have
"iterators" over a directory which present the directory in
alphabetical order, regardless of the order on the disk. But in
this case, the iterators actually point into an in memory copy,
in a vector, which has been sorted.) There is nothing wrong,
however, with, say, maintaining all of the elements in a vector,
and then several different sets of pointers to the element, each
sorted according to a different criteron.
--
James Kanze (Gabi Software) email:
ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34