Accessing pointed objects, pointer[0] vs ->

Sorry if this question has been asked before, but I could find it after doing a google search. I noticed, as I was going through the tutorials, that when accessing an object's methods, you could say pointer[0].method(); rather than pointer->method(); Is there a preferred way, as in is it best practice to use the -> rather than pointer[0] or even (*a).b? Thanks.
In my opinion the preferable way is to write pointer->method() instead of pointer[0].method(). The problem is that if pointer is an iterator then it can have no overloaded operator [].
They both technically do same thing. You should use the construct that most clearly indicates to the reader of your code what is going on.

If pointer is the name of an array, then use pointer[0].method() - using the [0] should indicate that you are accessing the first member of an array, while pointer[1] would be the second member, and so on.

If pointer is being used as a simple pointer (as the name suggests!), then use pointer->method() to make that clear.

Last edited on
Wow, you guys are just as fast as on the Stackexchange! Thanks for the answers. Also, sorry if this was meant to go in the beginners section, I didn't notice it as I accessed the forums through the side bar.
Last edited on
Topic archived. No new replies allowed.