Possible Duplicate:
C++ - Difference between (*). and ->?
What is the difference between this:
(*ptr).f();
and this:
ptr->f();
in c++ where the ptr is a pointer to C++ class which has a function f
?
Possible Duplicate:
C++ - Difference between (*). and ->?
What is the difference between this:
(*ptr).f();
and this:
ptr->f();
in c++ where the ptr is a pointer to C++ class which has a function f
?
There's no difference at all. (*ptr).f();
is the uglier way to do this.
Actually, if ptr
is some smart pointer and its operator*
and operator->
are overloaded and execute some side-effects, then you may have a problem with this. But this is really, really bad thing to do. It's as evil as #define true false
If ptr
is a normal pointer, then both are equivalent. ptr->f
is a short-cut to dereference the pointer (equivalent to (*ptr)
) and access the member of the dereferenced object (equivalent to .f
).
If ptr
is a class that overloads operator->
and operator*
, then they will each call different operator overloads, and so could have different behaviour.
Aside from stylistic/typing differences, there is no difference. It's exactly the same as (*ptr).member = 7;
vs ptr->member = 7;
when using a pointer to a structure or class.