Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Rules of thumb for <em>C++11:</em></h1> <p>Pass <strong><em>by value</em></strong>, except when </p> <ol> <li>you do not need ownership of the object and a simple alias will do, in which case you <strong><em>pass by <code>const</code> reference</em></strong>, </li> <li>you must mutate the object, in which case, use <strong><em>pass by a non-<code>const</code> lvalue reference</em></strong>,</li> <li>you pass objects of derived classes as base classes, in which case you need to <strong><em>pass by reference</em></strong>. (Use the previous rules to determine whether to pass by <code>const</code> reference or not.) </li> </ol> <p>Passing by pointer is virtually never advised. Optional parameters are best expressed as a <code>boost::optional</code>, and aliasing is done fine by reference.</p> <p>C++11's move semantics make passing and returning by value much more attractive even for complex objects.</p> <hr> <h1>Rules of thumb for <em>C++03:</em></h1> <p>Pass arguments <strong><em>by <code>const</code> reference</em></strong>, except when </p> <ol> <li>they are to be changed inside the function and such changes should be reflected outside, in which case you <strong><em>pass by non-<code>const</code> reference</em></strong></li> <li>the function should be callable without any argument, in which case you pass by pointer, so that users can pass <code>NULL</code>/<code>0</code>/<code>nullptr</code> instead; apply the previous rule to determine whether you should <strong><em>pass by a pointer to a <code>const</code> argument</em></strong></li> <li>they are of built-in types, which can be <strong><em>passed by copy</em></strong></li> <li>they are to be changed inside the function and such changes should <strong><em>not</em></strong> be reflected outside, in which case you can <strong><em>pass by copy</em></strong> (an alternative would be to pass according to the previous rules and make a copy inside of the function) </li> </ol> <p>(here, "pass by value" is called "pass by copy", because passing by value always creates a copy in C++03)</p> <hr> <p>There's more to this, but these few beginner's rules will get you quite far. </p>
    singulars
    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. 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