Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing copy assignment operator for Point class
    text
    copied!<p>Consider a point class, with declarations like:</p> <pre><code>// Point.h class Point { public: Point(); // Default constructor Point(const Point &amp; p); // Copy constructor Point &amp; operator=(Point source); // Copy assignment operator, needs implemented ~Point(); // Destructor // ... private: double m_x; // X coordinate double m_y; // Y coordinate }; </code></pre> <p>For the homework, the only thing I have left to implement is the copy assignment operator.<br> The canonical answer for how to do this is the <a href="https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom">copy-and-swap idiom</a>. </p> <p>Using the swap function for copy assignment solves one problem and creates another (how to implement the swap function).</p> <p>While I don't feel a need to provide a swap function, I wouldn't know how best to implement swap anyway. Is it to specialize <a href="http://en.cppreference.com/w/cpp/algorithm/swap" rel="nofollow noreferrer"><code>std::swap</code></a>? I know about neither namespaces nor template specialization yet.</p> <p>Formally, my question is two-fold:</p> <ol> <li>How should copy assignment be implemented? If it uses a swap function, how should I implement that?</li> <li>In the wild, would I simply not write the <a href="http://en.wikipedia.org/wiki/Rule_of_three_%28C++_programming%29" rel="nofollow noreferrer">Big Three members</a>, as the <code>Point</code> class is simply two numbers? Will the compiler write the operations correctly and optimally?</li> </ol>
 

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