Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are several cases to consider.</p> <h2>Parameter modified ("out" and "in/out" parameters)</h2> <pre><code>void modifies(T &amp;param); // vs void modifies(T *param); </code></pre> <p>This case is mostly about style: do you want the code to look like <em>call(obj)</em> or <em>call(&amp;obj)</em>? However, there are two points where the difference matters: the optional case, below, and you want to use a reference when overloading operators.</p> <h3>...and optional</h3> <pre><code>void modifies(T *param=0); // default value optional, too // vs void modifies(); void modifies(T &amp;param); </code></pre> <h2>Parameter not modified</h2> <pre><code>void uses(T const &amp;param); // vs void uses(T param); </code></pre> <p>This is the interesting case. The rule of thumb is "cheap to copy" types are passed by value — these are generally small types (but not always) — while others are passed by const ref. However, if you need to make a copy within your function regardless, you <a href="http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/" rel="noreferrer">should pass by value</a>. (Yes, this exposes a bit of implementation detail. <em>C'est le C++.</em>)</p> <h3>...and optional</h3> <pre><code>void uses(T const *param=0); // default value optional, too // vs void uses(); void uses(T const &amp;param); // or optional(T param) </code></pre> <p>There's the least difference here between all situations, so choose whichever makes your life easiest.</p> <h2>Const by value is an implementation detail</h2> <pre><code>void f(T); void f(T const); </code></pre> <p>These declarations are actually the <em>exact same function!</em> When passing by value, const is purely an implementation detail. <a href="http://codepad.org/ReB7ePO8" rel="noreferrer">Try it out:</a></p> <pre><code>void f(int); void f(int const) { /* implements above function, not an overload */ } typedef void NC(int); // typedefing function types typedef void C(int const); NC *nc = &amp;f; // nc is a function pointer C *c = nc; // C and NC are identical types </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload