Falmarri Falmarri - 1 year ago 84
C++ Question

Pointer dereference operator ( (*) vs -> )

Is there a general difference between doing




I saw this question in a comment on another question and thought I would ask it here. Although I just remembered that pretty much every operator in C++ can be overloaded, so I guess the answer will depend. But in general, is there a difference between doing one versus the other?

Answer Source

As "jamesdlin" already noted, the * and -> operators can be overloaded for class types.

And then the two expressions (*ptr).method() and ptr->method() can have different effect.

However, for the built-in operators the two expressions are equivalent.

The -> operator is more convenient when you're following a chain of pointers, because . has higher precedence than *, thus requiring a lot of ungrokkable parentheses.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download