4

Could someone explain to me what the "->" means in C++?

Examples if you can, they help me understand better. Thanks.

Bumrang
  • 356
  • 1
  • 3
  • 13
  • 2
    +1 I don't know why people are downvoting you. It's a basic question, but so what? That doesn't make it bad. – Scott Stafford Feb 12 '12 at 02:17
  • 1
    +1: It's a simple question, but it's very hard to search the web for such characters. You'd probably benefit from [a good book](http://stackoverflow.com/q/388242/78845), though. – johnsyweb Feb 12 '12 at 02:17
  • 4
    True, it is a basic question. But it's also a question that would *easily* be answered by looking at *any* book on C++ or C. Google is not the *only* research that people should do. – Nicol Bolas Feb 12 '12 at 02:19

3 Answers3

9

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;
Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • It is much more convenient. Here's a code snippet from some real code, given a pointer to a resource descriptor (`rd`), it gets the friendly name of the server that hosts the resource: `rd->resource->GetServer()->GetFriendlyName()`. The `GetServer` function returns a pointer to the server that hosts the resource. `resource` is a member of the resource descriptor structure. Think how ugly that would be in `(*).` form. – David Schwartz Feb 12 '12 at 02:22
  • as with almost all operators in C++ you should make the addendum that both `*` and `->` can be overloaded so `(*a).b` and `a->b` although usually the same have no guarantee of being equivalent unless `a` is a pointer. – PeterT Feb 12 '12 at 02:25
7

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.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • 1
    There is a conspicuous lack of mention of the term "pointer" in your answer. Yes, operator-> can be overloaded, but even then, that's only really done for objects that mimic pointers. – Nicol Bolas Feb 12 '12 at 02:20
  • @NicolBolas You do usually (that is, almost always) use the member access object on something that is either literally a pointer or an instance of a class that acts like a pointer. I do say that it accesses the "object `a` *points* to". – David Schwartz Feb 12 '12 at 02:24
  • Yes, you say that, but you then say you can read it as "`a` member access `b`". That's not possible if `a` is a naked pointer, since pointers don't actually have members. It's "`a` dereference, then member access `b`". And "dereference" doesn't make sense without talking about pointers. – Nicol Bolas Feb 12 '12 at 02:43
  • @NicolBolas Pointers don't have members, but what pointers access do have members. Hence it's a "member *access*" operation. (That is, it allows you to access a member.) – David Schwartz Feb 12 '12 at 02:52
3

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278