MtRock.cs Paradise 16

Qingshuan’s Programming Blog

[C++/.NET] When to use a reference

Posted by linuxprogram on January 28, 2008

Q:
When should I return a reference from a function?

A: Basically there are two situations:

  • The information being returned is a large enough object that returning a reference is more efficient than returning a copy. Just as it can be more efficient to pass large objects to functions by reference, it also can be more efficient to return large objects from functions by reference. Reference-return protocol eliminates the necessity of copying the object to a temporary location prior to returning.
  • The type of the function must be an l-value. Reference-return types can also be useful when the function must evaluate to an l-value. Most overloaded operators fall into this category, particularly the assignment operator.

Quoted from: http://www.codeguru.com/forum/showthread.php?t=316925

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>