Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This answer is more like an addition and a slight modification to the answers above.</p> <p>In some versions of Visual Studio (and possibly other compilers) there is a bug that is really annoying and doesn't make sense. So if you declare/define your <code>swap</code> function like this:</p> <pre><code>friend void swap(A&amp; first, A&amp; second) { std::swap(first.size, second.size); std::swap(first.arr, second.arr); } </code></pre> <p>... the compiler will yell at you when you call the <code>swap</code> function: </p> <p><img src="https://i.stack.imgur.com/OwEKE.jpg" alt="enter image description here"></p> <p>This has something to do with a <code>friend</code> function being called and <code>this</code> object being passed as a parameter.</p> <hr> <p>A way around this is to not use <code>friend</code> keyword and redefine the <code>swap</code> function:</p> <pre><code>void swap(A&amp; other) { std::swap(size, other.size); std::swap(arr, other.arr); } </code></pre> <p>This time, you can just call <code>swap</code> and pass in <code>other</code>, thus making the compiler happy:</p> <p><img src="https://i.stack.imgur.com/7XdNa.jpg" alt="enter image description here"></p> <hr> <p>After all, you don't <em>need</em> to use a <code>friend</code> function to swap 2 objects. It makes just as much sense to make <code>swap</code> a member function that has one <code>other</code> object as a parameter.</p> <p>You already have access to <code>this</code> object, so passing it in as a parameter is technically redundant. </p>
 

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