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?