[Insight-developers] Const/nonConst Iterator

Luis Ibanez luis.ibanez@kitware.com
Wed, 17 Apr 2002 13:51:25 -0400


Hi 

I ran into the following problem when trying to make the 
non-const iterator derive from the const-iterator:


1) the const iterator has a constructor that accepts a
    (Const ImageType *)


2) the non-const iterator has a constructor that accepts
    a non-const  (ImageType *).

3) the non-const iterator add the method Set()


The only two methods in the non-const version are the
constructor and the Set() method.  So, far everything is
quite good.


but... because the non-const iterator can still have access
to the constructor with const ImageType * it is possible to
write the following code without getting any compiler 
warnings:


ImageType::ConstPointer   myConstImage;

ImageConstIterator< ImageType >  it( myConstImage, region );
it.GoToBegin();

it.Set( 200 );  <== Here the constness of the image is violated !!



It seems that the non-const version of the iterator should not
derive from the const version but rather both of them should
derive from a common class.  That will also allow to manage
properly some ivars that should be const in one interator and
non-const in the other.



  Luis


--------------------------------------------------------

Lorensen wrote:
> Why not make the nonconst iterator a subclass of the const one, 
> overriding the appropriate methods. I
> would hold off on introducing a new concept like a walker...