list<CFoo>::iterator i = mylist.begin();
cout << i->m_bar1; // A
cout << (*i).m_bar2; // B
🔒 Locked
Difference when using derefered iterator?
Advertisement
Original Post
Quote:
Original post by Mizipzor
In this little snippet, is there any difference between A and B? When stepping through with the debugger they seem to return the same data, but somehow it seems my program behaves differently depending on which of them I use. :p
Yes, there may be a difference between A and B. It depends on how operator->() and operator*() are implemented in the iterator class. There is no way to tell without looking at the code for that class, however.
If 'i' were a simple unadorned plain vanilla garden-variety dumb pointer, there would be no difference between the two. Since it's not, it's impossible to say what the differences are without examining the code.
If your "list" class is std::list, chance are your standard library implementation is sane and the two expressions provide identical functionality.
--smw
Quote:
Original post by Bregma
Yes, there may be a difference between A and B. It depends on how operator->() and operator*() are implemented in the iterator class. There is no way to tell without looking at the code for that class, however.
Actually there is, if list<CFoo>::iterator is an iterator (very likely) then it must satisfy C++ requirements for iterators. One of these requirements are:
"All iterators i for which the expression (*i).m is well-defined, support the expression i->m with the same semantics as (*i).m." (directly from the standard)
As for which one to use, it's personal preference. Personally I use -> because I think it expresses what I want to do more clearly, which is to access a member of the object pointed to by i.
Quote:
Original post by RossguyIntroduced only for syntactic sugar
ORLY?
#include <iostream>#include <memory>struct A{ void sayit() { std::cout << "Hello World!" << std::endl; }};struct B{ void sayit() { std::cout << "Go away!" << std::endl; }};struct C{ std::auto_ptr<A> operator -> () { return std::auto_ptr<A>( new A() ); } B operator * () { return B(); }};int main(){ C x; x->sayit(); (*x).sayit(); return 0;}Quote:
Difference when using derefered iterator?
It is not derefered, it is dereferenced! (its a big difference between these two words)
Quote:
Original post by bubu LV Quote:
Original post by RossguyIntroduced only for syntactic sugar
ORLY?
YARLY!
That is why they were introduced, but they can be used for more. Postfix ++ was also only introduced as syntactic sugar, but it can be used for other things.
Quote:
From The C++ Programming Language, by Bjarne Stroustup
For ordinary pointers, use of -> is synonymous with some uses of unary * and []. Given
Y * p ;
it holds that
p->m == (*p).m == p[0].m
...
If you provide more than one of these operators, it might be wise to provide the equivalence, just as it is wise to ensure that ++x and x +=1 have the same effect as x =x +1 for a simple variable x of some class if ++, +=, =, and + are provided.
Quote:
Original post by CTar Quote:
Original post by bubu LV Quote:
Original post by RossguyIntroduced only for syntactic sugar
ORLY?
YARLY!
That is why they were introduced, but they can be used for more. Postfix ++ was also only introduced as syntactic sugar, but it can be used for other things.
Thanks :) That could have gone ugly quickly... :)
Thanks everyone for your responses, now I know. And since -> is clearly a step towards cleaner code, I will use that frow now on. :)
And an extra thanks for pointing that out. :)
Quote:
Original post by bubu LV Quote:
Difference when using derefered iterator?
It is not derefered, it is dereferenced! (its a big difference between these two words)
And an extra thanks for pointing that out. :)
I derefered even a word?
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement