46

Possible Duplicates:
What is the difference between the dot (.) operator and -> in C++?
What is the arrow operator (->) synonym for in C++?

The header says it all.

What does -> mean in C++?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Eric Brotto
  • 53,471
  • 32
  • 129
  • 174

7 Answers7

69

It's to access a member function or member variable of an object through a pointer, as opposed to a regular variable or reference.

For example: with a regular variable or reference, you use the . operator to access member functions or member variables.

std::string s = "abc";
std::cout << s.length() << std::endl;

But if you're working with a pointer, you need to use the -> operator:

std::string* s = new std::string("abc");
std::cout << s->length() << std::endl;

It can also be overloaded to perform a specific function for a certain object type. Smart pointers like shared_ptr and unique_ptr, as well as STL container iterators, overload this operator to mimic native pointer semantics.

For example:

std::map<int, int>::iterator it = mymap.begin(), end = mymap.end();
for (; it != end; ++it)
    std::cout << it->first << std::endl;
Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 13 years later and this is the only answer here that actually explains it for me. Thanks a lot! – Chris S Jul 27 '23 at 13:35
54

a->b means (*a).b.

If a is a pointer, a->b is the member b of which a points to.

a can also be a pointer like object (like a vector<bool>'s stub) override the operators.

(if you don't know what a pointer is, you have another question)

J-16 SDiZ
  • 26,473
  • 4
  • 65
  • 84
33
  1. Access operator applicable to (a) all pointer types, (b) all types which explicitely overload this operator
  2. Introducer for the return type of a local lambda expression:

    std::vector<MyType> seq;
    // fill with instances...  
    std::sort(seq.begin(), seq.end(),
                [] (const MyType& a, const MyType& b) -> bool {
                    return a.Content < b.Content;
                });
    
  3. introducing a trailing return type of a function in combination of the re-invented auto:

    struct MyType {
        // declares a member function returning std::string
        auto foo(int) -> std::string;
    };
    
Paul Michalik
  • 4,331
  • 16
  • 18
  • 6
    +1 for the return types. – Puppy Nov 06 '10 at 14:03
  • Don't forget #2 applies to all deduced return types (lambdas and non-lambdas). –  Nov 06 '10 at 14:22
  • Added to the abve enumeration. Don't know why the code examples do not show up correctly, if anybody knows what's going on, please feel free to correct the formatting. – Paul Michalik Nov 07 '10 at 11:26
  • After a list element, code has to be indented 8 spaces instead of 4. I also removed the trailing return type from the lambda, it's deduced. – GManNickG Nov 07 '10 at 11:30
  • 1
    @GMan Thanks, but I added it back, since otherwise that becomes quite irrelevant with respect to the OP's question :) I could not (rapidly) come up with a lambda whose return type cannot be deduced, so that trivial one shall serve as an example... – Paul Michalik Nov 07 '10 at 11:57
  • @paul_71: Oh, right. Though you've misplaced it. – GManNickG Nov 07 '10 at 18:25
  • @GMan Yeah, and corrected again... Now we keep it closed :-) – Paul Michalik Nov 08 '10 at 06:23
6

x->y can mean 2 things. If x is a pointer, then it means member y of object pointed to by x. If x is an object with operator->() overloaded, then it means x.operator->().

Chris Card
  • 3,216
  • 20
  • 15
  • No. If x is an object with operator-> overloaded, it means x.operator->(), and if the return value also supports operator->, then it means x.operator->().operator->(). Operator-> takes no arguments. – Puppy Nov 06 '10 at 14:04
3

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Member_and_pointer_operators

a -> b is member b of object pointed to by a

Core Xii
  • 6,270
  • 4
  • 31
  • 42
2

The -> operator, which is applied exclusively to pointers, is needed to obtain the specified field or method of the object referenced by the pointer. (this applies also to structs just for their fields)

If you have a variable ptr declared as a pointer you can think of it as (*ptr).field.

A side node that I add just to make pedantic people happy: AS ALMOST EVERY OPERATOR you can define a different semantic of the operator by overloading it for your classes.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • 1
    It does not apply exclusively to pointers because it can be overloaded as a class operator. The shared pointer component does this as do iterators for containers in the standard library. – Jon Trauntvein Nov 06 '10 at 14:02
  • 2
    yes, of course it can be overloaded (as every operator) but I guess the OP needed to know the original semantics of the operator.. – Jack Nov 06 '10 at 14:06
1

member b of object pointed to by a a->b

mepcotterell
  • 2,670
  • 2
  • 21
  • 28