Search code examples
c++iteratordereference

What is a difference between *iterator.fun() and (*iterator).fun()


I have simple code

#include <iostream>
#include <set>

using namespace std;
class Example
{
    string name;
public:
    Example(string name)
    {
        this -> name = name;
    }
    string getName() const {return name;}
};
bool operator<(Example a, Example b)
{
    return a.getName() < b.getName();
}
int main()
{
    set<Example> exp;
    Example first("first");
    Example second("second");
    exp.insert(first);
    exp.insert(second);
    for(set<Example>::iterator itr = exp.begin(); itr != exp.end(); ++itr)
    {
        //cout<<*itr.getName(); - Not working
        cout<<(*itr).getName()<<endl;
    }

}

I wonder why *itr.getName() doesn't work but (*itr).getName() works fine. What is the difference between *itr and (*itr) in this case?


Solution

  • C++ has rules for operator precedence. According to these rules, the member access operator . has higher precedence than the dereference operator unary *.

    Therefore, the expression

    *itr.getName()
    

    Is interpreted as though it had the parentheses:

    *(itr.getName())
    

    That is, it tries to evaluate itr.getName() and then dereference that. itr doesn't have any such member getName, so this doesn't compile. However, you can use the parentheses yourself to write:

    (*itr).getName()
    

    This dereferences itr first, and then accesses the getName member of the resulting Example. You can also use the dereference-and-member-access operator -> as follows:

    itr->getName() // Same meaning as (*itr).getName()