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

Tags: pointers c++
By : P-A
Source: Stackoverflow.com

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

By : P-A


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.

By : P-A

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

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


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

By : Tetha

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

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

This video can help you solving your question :)
By: admin