Re: std::list iterators and swapping
On Jul 26, 1:23 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
James Kanze wrote:
The simplest solution (in my mind, anyway) uses inheritance:
you have a BaseNode with the pointers, and a DerivedNode
which contains the memory for the data.
Do you mean that you have a BaseNode object as member (as the
end node),
Yes.
but an iterator pointing to it would have a
DerivedNode type pointer pointing to this BaseNode type
object?
No. All pointers are always to the BaseNode; the element()
function of my iterator (which corresponds more or less to the
operator*() of a standard iterator) used a static_cast (actually
a C style cast, back then) to convert the pointer, e.g.:
return static_cast< DerivedNode* >( myPtr )->data ;
Is this even allowed?
Having a pointer to base which actually points to a derived is
certainly allowed:-). And templates (actually <generic.h>, back
then) guarantee the type, so the static_cast downcast is
guaranteed (unless you're at the end, of course).
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34