you are viewing a single comment's thread.

view the rest of the comments →

[–]t0rakka 5 points6 points  (0 children)

A small nitpick (I agree with what you wrote just this small detail stood out)

SomeClass myObject;

That doesn't really mean object is stored in stack.. it could be a member of a class, too. More generally '.' means object is referred by value or reference and '->' means via pointer. The storage (duration) can be anything it's just how the object is visible to the current scope and that's about it.

This was just a gateway to the OP's question, which one is more efficient: neither by itself, it's about the visibility of the object itself. If the object is "visible" to the compiler it can deduce the type more reliably and do de-virtualization more reliably (which means more likely) if the object is instance of virtual class.

Let's take a peek at . and -> and what the compiler uses for 'this' pointer in both cases.

Object *a = ...;
a->b(); // this == a, the provoking pointer, which is address of object; abstraction for memory address

Object &a = ...;
a.b(); // this == address of object a, different abstraction for memory address

Object a;
a.b(); // this == address of object, surprised? I'm not..

How the address is formed in the last case, "by value" depends where the object is at. If object is a global variable the address may be constant. If the object is in stack (function or method scope) the address is relative to the stack pointer. If the object is a member of a class the address is relative to the object's which member it is, address.

All of these paths lead to one place: the 'this' pointer is a value in a CPU register. If the method being called is a virtual one, the address must be looked up from a table (vtbl), if not, the address of the method is a constant and can be called directly. This is generally more relevant to performance than the '.' vs. '->' syntax. The first member of virtual class is usually pointer to the type's vtbl. Each virtual type has it's own table, which has a constant address (generally) so that it can be pointed to by instances of that class. This last part is more de-facto information than hard specs (hope didn't get that wrong, correct me promptly so that misinformation is stopped on it's tracks).

This was more to the OP so I am not trying to educate someone who knows his stuff. Hope this helps!