When iterating over an vector (or other container) of pointers, is there any difference between and/or over advantage using:
for (it = v.begin(); it != v.end(); ++it) {
(*it)->method();
}
or
for (it = v.begin(); it != v.end(); ++it) {
(**it).method();
}
In the C language, there is no difference. However, in C++, the
->operator can be overloaded, whereas the member selection.operator cannot.So in
(*foo)->bar*foocould designate a class object which acts as a smart pointer, though this won’t happen iffoois an iterator over a standard C++ container of pointers, which means that*fooevaluates to a pointer.And in
(**foo).bar,**foohas to be a class object with a member calledbar(which is accessible).The unary
*can also be overloaded (which is how the iteratorfoo, a class object, returns the object which it references).In other words, the expressions can diverge in meaning, but if
*foois a pointer to a class/struct, then the equivalence inherited from the C language applies:(*ptr).memberis equivalent toptr->member.