Re: In map iterator is there a difference between (*iter).second and iter->second?
"Mike Wahler" <mkwahler@mkwahler.net> writes:
"puzzlecracker" <ironsel2000@gmail.com> wrote in message
news:8114115f-8075-4f7b-891d-298b48b9a383@56g2000hsm.googlegroups.com...
I see that a lot of former in the code, and wonder if there is a
technical reason for that
You're asking about language syntax, this is
not specific to map or iterators.
Given a pointer to a class or struct:
struct T
{
int member;
};
and a pointer to one of these structs:
T obj;
T *p(&obj);
The two expressions:
(*p).member
and
p->member
have exactly the same meaning.
The '->' form is 'shorthand' for the (*). form.
Yes, but given a different class, they may be different:
#include <iostream>
class A {
public: int x;
};
class P {
public:
A a1;
A a2;
A& operator*(){ return(a1); }
A* operator->(){ return(&a2); }
};
using namespace std;
int main(void){
P p;
p.a1.x=1;
p.a2.x=2;
cout<<"p->x = "<<p->x<<" ; (*p).x = "<<(*p).x<<endl;
return(0);
}
/*
-*- mode: compilation; default-directory: "~/src/tests-c++/" -*-
Compilation started at Wed Aug 20 11:40:06
SRC="/home/pjb/src/tests-c++/memb.c++" ; EXE="memb" ; g++ -g3 -ggdb3 -o ${EXE} ${SRC} && ./${EXE} && echo status = $?
p->x = 2 ; (*p).x = 1
status = 0
Compilation finished at Wed Aug 20 11:40:07
*/
Why someone used the latter over the former I
don't know. Perhaps a 'style' issue, or possibly
that's what a code generator created.
Or perhaps they mean different things.
--
__Pascal Bourguignon__