[Insight-users] ConstIterator inaccessible from non-const ListSample

Zachary Pincus zpincus at stanford.edu
Tue, 13 Apr 2004 18:00:22 -0700


Hello,

I'm using a ListSample, and recently decided to iterate through it. 
Being a responsible type, and knowing that I didn't want to change the 
list, I thought that using a ListSample::ConstIterator would be the 
best way to go.

Unfortunately, it is not possible to get a ConstIterator easily out of 
a non-const ListSample object. (And quite possibly, other objects that 
share the following interface convention.)

The problem/feature is thus: (from the class definition in the header)

ConstIterator Begin() const {...}
Iterator Begin() {...}

So the Begin() that returns a ConstIterator is only called when the 
ListSample object itself is const. With a non-const ListSample (which 
mine is, because I just constructed it), there's no way, aside from an 
ugly const_cast<> construction, to get a ConstIterator from the Begin() 
function.

Is this a design oversight or a specific feature? Or am I missing "the 
right way" to do this?

Thanks,

Zach Pincus
Department of Biochemistry and Program in Biomedical Informatics
Stanford University School of Medicine