What is the difference between the dot (.) operator and -> in C++?


Question

What is the difference between the dot (.) operator and -> in C++?

1
293
9/1/2010 7:25:46 AM

Accepted Answer

The following two expressions are equivalent:

a->b

(*a).b

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

130
10/21/2008 10:10:09 AM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon