Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three ways to pass arguments: <strong>by value</strong>, <strong>with pointers</strong>, and <strong>by reference</strong>.</p> <p><strong>By value</strong>: A new copy of the argument will be created. For big objects like <code>std::string</code> this could be expensive. For small things like <code>int</code>, it doesn't matter.</p> <pre><code>void myFunc(std::string str) { ... } std::string mySuperLongString = getSuperLongString() myFunc(mySuperLongString) // will make a copy of the long string. Expensive. </code></pre> <p><strong>With pointers</strong>: When passing a pointer, you're just passing the address of a piece of data. Really, the pointer is passed by value, but because you're just passing an address, it's a light operation.</p> <pre><code>void myFunc(std::string *str) { ... } std::string mySuperLongString = getSuperLongString() myFunc(&amp;mySuperLongString) // Pass the address of the string. Light operation </code></pre> <p><strong>With references</strong>: It's very similar to using pointers except that you have some extra safety checks. For example, you can't reassign a reference after assigning it once, and you can treat a reference as simply another name for the thing you're working with (i.e. you don't need to use the dereference operators <code>*</code> and <code>-&gt;</code> like with pointers). Using references is <em>just as lightweight as using pointers</em>, but safer. This is the preferred way of passing arguments C++.</p> <pre><code>void myFunc(std::string&amp; str) { ... } std::string mySuperLongString = getSuperLongString() myFunc(mySuperLongString) // Pass a reference to the string. Light operation </code></pre>
    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. This table or related slice is empty.
    1. 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