fungus wrote:
Quote:
I mentioned earlier to day that I was moving some code
from VC++6 to VC++2005 and having trouble with the
new iterators. There's all sorts of problems cropping
up in the code thanks to this change.
>
a) With pointer-iterators you could set an iterator
to null to mark it as invalid, you can't do that
any more.
>
An iterator by itself is largely useless. It needs to be paired with
another iterator to form a range. Then you can set the first iterator
to the second to indicate an empty range.
Alternatively, it could be paired with the container from which it
originates, and you could set it to the containers end() to indicate
invalid.
Quote:
b) You can't use const_cast with iterators.
>
Can you give an example of when you might want to? If you need to
modify the value an iterator "points" to, then the iterator shouldn't
have been a const_iterator to begin with, so the correct solution is to
change its type.
Quote:
I can hack (b) by using "thing.begin()+n"
to make a new iterator but dealing with (a) is
a real pain. Unless there's a hack for this
I'm going to have to have a flag associated
with the iterator to mark it as valid/invalid.
>
So...what's the rationale behind changing from
simple pointers to fancy iterator objects? Was
there a good reason for doing this? To me it
seems that pointers are a much more natural
(and useful) choice.
>
I don't know what rationale the developers of your platform had, but, as
you've seen, you should never rely on the underlying type of an
iterator. Each iterator concept has a very specific set of expressions
for which it has valid semantics. If you use them in any expression not
in that set, your program ceases to be valid C++, and may or may not
compile on a given platform.
If you don't have a copy of the standard handy, the following URL has a
good discussion of the five iterator concepts supported in C++:
http://www.sgi.com/tech/stl/Iterators.html Quote:
>
Also, something I've been meaning to ask for
a while: Why is the default access for a class
"private"? Surely "public" makes much more sense.
>
This sounds suspiciously trollish, but I'll answer anyway.
Object oriented programming is all about interfaces, not
implementations. How the data in an object is stored is an
implementation detail that clients of that object should not have to
worry about.
Consider the standard library containers, as an example. They all have
a size() method to tell you how many elements they contain. Is the size
stored internally as a variable, or is it computed when you ask for it?
The correct answer is, you don't know and shouldn't have to care.
With either implementation, the interface stays the same, and you could
even change the implementation without having to change client code.
But, if you REALLY want the default to be public, use a struct. Other
than default accessibility of their members, there is no difference
between a class and a struct.
--
Alan Johnson