Coming from other C-derived languages (like Java or C#) to C++, it is at first very confusing that C++ has three ways to refer to members of a class: a::b, a.b, and a->b. When do I use which one of these operators?
(Note: This is meant to be an entry to Stack Overflow's C++ FAQ. If you want to critique the idea of providing an FAQ in this form, then the posting on meta that started all this would be the place to do that. Answers to that question are monitored in the C++ chatroom, where the FAQ idea started out in the first place, so your answer is very likely to get read by those who came up with the idea.)
The three distinct operators C++ uses to access the members of a class or class object, namely the double colon
::, the dot., and the arrow->, are used for three different scenarios that are always well-defined. Knowing this allows you to immediately know quite a lot aboutaandbjust by looking ata::b,a.b, ora->b, respectively, in any code you look at.a::bis only used ifbis a member of the class (or namespace)a. That is, in this caseawill always be the name of a class (or namespace).a.bis only used ifbis a member of the object (or reference to an object)a. So fora.b,awill always be an actual object (or a reference to an object) of a class.a->bis, originally, a shorthand notation for(*a).b. However,->is the only of the member access operators that can be overloaded, so ifais an object of a class that overloadsoperator->(common such types are smart pointers and iterators), then the meaning is whatever the class designer implemented. To conclude: Witha->b, ifais a pointer,bwill be a member of the object the pointerarefers to. If, however,ais an object of a class that overloads this operator, then the overloaded operator functionoperator->()gets invoked.The small print:
class,struct, orunionare considered "of class type". So the above refers to all three of them.T*&) are rarely ever used.