For one reason or another, I\'m forced to provide both a copy constructor and an operator= for my class. I thought I didn\'t need operator= if I defined a copy
Yes, this is good practice and should (almost) always be done. In addition toss in a destructor and default constructor (even if you make it private).
In James Coplien's 1991 book Advanced C++, this is described as part of "Orthodox Canonical Form". In it, he advocates for a default constructor, a copy constructor, the assignment operator and a destructor.
In general, you must use the orthodox canonical form if:
- You want to support assignment of object of the class, or want to pass those objects as call-by-value parameters to a function, and
- The object contains pointers to objects that are reference-counted, or the class destructor performs a
deleteon a data member of the object.You should use the orthodox canonical form for any nontrivial class in a program, for the sake of uniformity across classes and to manage the increasing complexity of each class over the course of program evolution.
Coplien offers pages of reasons for this pattern and I couldn't do them justice here. However, a key item that has already been touched on is the ability to clean up the object that is being overwritten.