tags:

views:

80

answers:

2

if I have

list<NS*> v;  
typename list<NS*>::iterator it;   
for(it = v.begin();it!=v.end();++it){  
  cout<<**it.ns_member1<<endl;    // does not compile  
  NS ns = **it;  
  cout<<ns.ns_member1<<endl;      // this compiles.  
}

Why so?

+2  A: 

Try (**it).ns_member1.

Otherwise, the dereferencing would be done after trying to evaluate it.ns_member1. It's like 3*(1+2) vs 3*1+2.

ypnos
+1  A: 

Dereference (the '*') has a lower precedence than the '.' operator, so this line:

cout<<**it.ns_member1<<endl;

Works out like this:

cout << (**(it.ns_member1)) <<endl; // ERROR

I would suggest doing it like this:

cout << (*it)->ns_member1 << endl;

There is really no need to use the dereference operator twice, once followed by the '->' operator will do the same thing and should read clearer to most people.

HTH.

0xC0DEFACE