Could someone explain to me what the "->" means in C++?
Examples if you can, they help me understand better. Thanks.
Could someone explain to me what the "->" means in C++?
Examples if you can, they help me understand better. Thanks.
 
    
    It's a shortcut for dereference followed by property access (or method invocation).
In code, here are some examples of this equivalence:
Foo *foo;
// field access
foo->bar = 10;
(*foo).bar = 10;
// method invocation
foo->baz();
(*foo).baz();
This is especially convenient when you have a long sequence of these. For example, if you have a singly linked list data structure in which each element has a pointer to the next, the following are equivalent ways of finding the fifth element (but one looks much nicer):
linked_list *head, *fifth;
fifth = head->next->next->next->next;
fifth = (*(*(*(*head).next).next).next).next;
 
    
    It's often called the "member access" operator. Basically, a->b is a nicer way to write (*a).b. You can think of a->b as "access the b member/function in the object a points to". You can read it aloud (or think it to yourself) as "a member access b".
In a random sample of structured C++ code I just checked (from several different projects written by different people), 10% of lines of code (not counting headers) contained at least one member access operator.
 
    
    The -> operator is used with a pointer (or pointer-like object) on the LHS and a structure or class member on the RHS (lhs->rhs).  It is generally equivalent to (*lhs).rhs, which is the other way of accessing a member.  It is more convenient if you ignore the Law of Demeter and need to write lhs->mid->rhs (which is generally easier to read than (*(*lhs).mid).rhs).
You can overload the -> operator, and smart pointers often do.  AFAIK You cannot overload the . operator.
