Consider a point class, with declarations like:
// Point.h
class Point {
public:
Point(); // Default constructor
Point(const Point & p); // Copy constructor
Point & operator=(Point source); // Copy assignment operator, needs implemented
~Point(); // Destructor
// ...
private:
double m_x; // X coordinate
double m_y; // Y coordinate
};
For the homework, the only thing I have left to implement is the copy assignment operator.
The canonical answer for how to do this is the copy-and-swap idiom.
Using the swap function for copy assignment solves one problem and creates another (how to implement the swap function).
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 std::swap
? I know about neither namespaces nor template specialization yet.
Formally, my question is two-fold:
- How should copy assignment be implemented? If it uses a swap function, how should I implement that?
- In the wild, would I simply not write the Big Three members, as the
Point
class is simply two numbers? Will the compiler write the operations correctly and optimally?