Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The difference is one, as you say, takes a typical int array <code>a[]</code>, but how would you know the size? So this version requires the user to send it to the function with <code>n</code> as the number of elements. In your <code>Array</code> class you provide a size so there's no need for it. In general you're providing overloads for multiple situations. </p> <p>I'm not sure what you mean by replacing <code>int a[]</code> w/ <code>&amp;arr</code>, the signature is there, use what was given to you unless you're <em>supposed</em> to change it.</p> <p>If you go back to your question about the <code>Array</code> class you can see an answer which uses the reference just as you would normally, i.e,</p> <pre><code>template &lt;class T&gt; Array&lt;T&gt;::Array(const Array &amp;other) : size(other.size), arr(new T[size]) { // ^^^^^^ for (int i = 0; i &lt; size; ++i) arr[i] = other.arr[i]; } // ^^^^^ </code></pre> <p>now apply it to this situation.</p> <p>Also,</p> <pre><code>template &lt;class T&gt; void selectionSort(Array&lt;T&gt; &amp;a) { // I'm not sure I understand what this is supposed to do that's different from the above selectionSort. // I know that &amp; is the reference operator, so should I just return whatever &amp;a is? } </code></pre> <p>You won't be returning anything here considering <code>void</code> as the return and the use of the reference. You pass by reference as opposed to by value so that what you do in the function is persistent. You <em>could</em> choose to pass back the sorted array and not use a reference but I'm fairly certain it'd be slower overall considering the assignment and copy. That's why the example from your other question is using <code>const Array &amp;other</code>. It prevents the entire array, which may be large, from being copied and sent to the function as well as being changed.</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. 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