c++ - Returning a pointer by reference -


i have seen c++ looks this:

const foo*& ptr = getpointertofoo(); 

which seems same job as:

const foo* ptr = getpointertofoo(); 

or even:

foo* ptr = getpointertofoo(); 

what use of & in first example? pass value returned getpointertofoo() reference? use of this? make run faster pointer not need copied? if so, seems rather complicated implementation small increase in speed... or there other reasons use & here?

  • const foo*& ptr: reference makes pointer mutable. caller can modify internal pointer (e.g. nullptr), not internal content (which useless).
  • const foo* ptr: pointer immutable content. caller can not modify internal pointer.
  • foo* ptr: pointer mutable content. caller can modify internal content, not internal pointer.

note: returning pointer reference indirection , makes code not faster.

note: assume 3 distinct getpointertofoo() in question.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -