Possible Duplicate:
ptr->hello(); /* VERSUS */ (*ptr).hello();
I am learning C++ and my question is if there is any difference between using the arrow operator (->) or dereferencing the pointer * for calling a function.
These two cases illustrate my question.
Class* pointer = new Class();
(*pointer).Function(); // case one
pointer->Function(); // case two
What is the difference?
Given
Then
dereferences the pointer, and calls the member function
Functionon the referred to object. It does not use any overloaded operator. Operators can’t be overloaded on raw pointer or built-in type arguments.This does the same as the first one, using the built-in
->becausepointeris a raw pointer, but this syntax is better suited for longer chains of dereferencing.Consider e.g.
versus
The
->notation is also more obvious at a glance.