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

moorthy picture moorthy · Aug 6, 2009 · Viewed 221k times · Source

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

Answer

SwDevMan81 picture SwDevMan81 · Aug 6, 2009

foo->bar() is the same as (*foo).bar().

The parenthesizes above are necessary because of the binding strength of the * and . operators.

*foo.bar() wouldn't work because Dot (.) operator is evaluated first (see operator precedence)

The Dot (.) operator can't be overloaded, arrow (->) operator can be overloaded.

The Dot (.) operator can't be applied to pointers.

Also see: What is the arrow operator (->) synonym for in C++?