tags:

views:

5149

answers:

6

I know it, forgets it and relearn it again. Time to write it down.

+26  A: 

The following two expressions are equivalent:

a->b

(*a).b

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

Greg Hewgill
Overloading issues are a lot less unusual than you think. Not long ago, STL implementors had no overloaded `->` operator for some iterator types so you *had* to use `*.`. Many libraries define them inconsistently. Becomes really annoying when you work with templates and don't know the precise type.
Konrad Rudolph
After your edit I think your post summons it in a good way.
P-A
A: 

Points to.

Example: this->foo;

Read as: This points to foo.

MrValdez
+21  A: 

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.

Konrad Rudolph
+1  A: 

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

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

becomes:

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

Tetha
+4  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:

(*p).method();

Which could be written with the arrow operator:

p->method();

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:

*(p.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.

P-A