Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>I want to ask about passing pointers between functions or between objects or returning them .. I heard that passing or returning pointers in general (whether they point to an array or an object or whatever they are pointing at) isn't safe and the output isn't guaranteed.</p> </blockquote> <p>Where did you get that from? In general, it's not true and of course you can pass around pointers.</p> <p>The tricky thing is managing <em>ownership</em> of (heap-allocated) objects, since somewhere you have to release the memory again. In other words: When you allocate memory with <code>new</code>, you will have to free it again with <code>delete</code>.</p> <p>Example:</p> <pre><code>A* function1(A* a) { return a; } B* function2(B* b) { return new B(b); } </code></pre> <p><code>function1</code> returns an existing pointer. Whoever <em>owned</em> the <code>A</code> object passed in will also own the returned one, as it is the same. This needs to be documented since this knowledge is essential for using <code>function1</code>!</p> <p><code>function2</code> creates a new object of class <code>B</code> by coping its input argument. Whoever calls <code>function2</code> will <em>own</em> the returned object and will be responsible to <code>delete</code> it when it's done. Again, this needs to be documented!</p> <blockquote> <p>also, I read in a tutorial that everything in c++ is pass by value? isn't passing pointers is called pass by reference?</p> </blockquote> <p>Technically, passing pointers is pass-by-value since the <em>pointer itself</em> gets copied. But since a pointer is a "reference type", you essentially get pass-by-reference with that.</p> <p>Note that C++ also knows references (<code>int&amp;</code>), which really is pass-by-reference.</p>
    singulars
    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.
 

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