What can I use instead of the arrow operator, `->`?


C++ Problem Overview

What is the arrow operator (->) a synonym for?

C++ Solutions

Solution 1 - C++

The following two expressions are equivalent:



(subject to operator overloading, as Konrad mentions, but that's unusual).

Solution 2 - C++

a->b is generally a synonym for (*a).b. The parenthesises here are necessary because of the binding strength of the operators * and .: *a.b wouldn't work because . binds stronger and is executed first. This is thus equivalent to *(a.b).

Beware of overloading, though: Since both -> and * can be overloaded, their meaning can differ drastically.

Solution 3 - C++

The C++-language defines the arrow operator (->) as a synonym for dereferencing a pointer and then use the .-operator on that address.

For example:

If you have a an object, anObject, and a pointer, aPointer:

SomeClass anObject = new SomeClass();
SomeClass *aPointer = &anObject;

To be able to use one of the objects methods you dereference the pointer and do a method call on that address:


Which could be written with the arrow operator:


The main reason of the existents of the arrow operator is that it shortens the typing of a very common task and it also kind of easy to forgot the parentheses around the dereferencing of the pointer. If you forgot the parentheses the .-operator will bind stronger then *-operator and make our example execute as:

*(aPointer.method()); // Not our intention!

Some of the other answer have also mention both that C++ operators can be overload and that it is not that common.

Solution 4 - C++

In C++0x, the operator gets a second meaning, indicating the return type of a function or lambda expression

auto f() -> int; // "->" means "returns ..."

Solution 5 - C++

I mostly read it right-to-left and call "in"

foo->bar->baz = qux->croak


"baz in bar in foo becomes croak in qux."

Solution 6 - C++

-> is used when accessing data which you've got a pointer to.

For example, you could create a pointer ptr to variable of type int intVar like this:

int* prt = &intVar;

You could then use a function, such as foo, on it only by dereferencing that pointer - to call the function on the variable which the pointer points to, rather than on the numeric value of the memory location of that variable:


Without the parentheses here, the compiler would understand this as *(ptr.foo()) due to operator precedence which isn't what we want.

This is actually just the same as typing


As the ->dereferences that pointer, and so calls the function foo() on the variable which the pointer is pointing to for us.

Similarly, we can use -> to access or set a member of a class:

myClass* ptr = &myClassMember;
ptr->myClassVar = 2; 

Solution 7 - C++

You can use -> to define a function.

auto fun() -> int
return 100;

It's not a lambda. It's really a function. "->" indicates the return type of the function.


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionP-AView Question on Stackoverflow
Solution 1 - C++Greg HewgillView Answer on Stackoverflow
Solution 2 - C++Konrad RudolphView Answer on Stackoverflow
Solution 3 - C++P-AView Answer on Stackoverflow
Solution 4 - C++Johannes Schaub - litbView Answer on Stackoverflow
Solution 5 - C++TethaView Answer on Stackoverflow
Solution 6 - C++Tryb GhostView Answer on Stackoverflow
Solution 7 - C++ZhangView Answer on Stackoverflow