c++ - Is (*i).member less efficient than i->member -


having

struct person {    string name; };  person* p = ... 

assume no operators overloaded.


which more efficient (if any) ?

(*p).name vs. p->name

somewhere in of head hear bells ringing, * dereference operator may create temporary copy of object; true?


the background of question cases this:

person& person::somefunction(){     ...     return *this; } 

and began wonder, if changing result person* , last line return this make difference (in performance)?

when return reference, that's same passing pointer, pointer semantics excluded.
pass sizeof(void*) element, not sizeof(yourclass).

so when that:

person& person::somefunction(){     ...     return *this; } 

you return reference, , reference has same intrinsic size pointer, there's no runtime difference.

same goes use of (*i).name, in case create l-value, has same semantics reference (see here)


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -