47

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++?

2
  • Which language are you familiar with?
    – Vlad
    Commented Nov 6, 2010 at 13:58
  • 1
    Not sure what you exactly mean about "->" . It is just a deferencing stuff for accessing member variables and functions. Hope this helps. Commented Nov 6, 2010 at 13:59

7 Answers 7

71

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;
1
  • 13 years later and this is the only answer here that actually explains it for me. Thanks a lot!
    – Chris S
    Commented Jul 27, 2023 at 13:35
53

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)

6
  • Unless it's a class with the operator overloaded.
    – Bill Lynch
    Commented Nov 6, 2010 at 14:14
  • 4
    It's a shame this has so many votes, since overloading the operator is common and an important piece of the answer. Commented Nov 6, 2010 at 14:21
  • 3
    I don't know what (*a).b means. Commented Nov 6, 2010 at 14:23
  • 4
    @Steve: Anyone that overloads a.operator->() to be different from &*a is evil.
    – Roger Pate
    Commented Nov 6, 2010 at 14:23
  • 1
    Steve, I also agree. While I appreciate the effort from J-16 SDiZ it seems like all the votes are probably coming from those who already understand clearly the answer. I will most likely accept an answer that is more descriptive. Commented Nov 6, 2010 at 14:24
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;
    };
    
7
  • 7
    +1 for the return types.
    – Puppy
    Commented Nov 6, 2010 at 14:03
  • Don't forget #2 applies to all deduced return types (lambdas and non-lambdas).
    – Roger Pate
    Commented Nov 6, 2010 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. Commented Nov 7, 2010 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
    Commented Nov 7, 2010 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... Commented Nov 7, 2010 at 11:57
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->().

1
  • 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
    Commented Nov 6, 2010 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

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.

2
  • 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. Commented Nov 6, 2010 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
    Commented Nov 6, 2010 at 14:06
1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.