Inspired by this question.
Usually the reason to make copy-constructor and assignment operator private
is to make the class non-copyable so that objects can only be created and destroyed, but not copied - most of the times it is because copying them would make no sense. In such cases the copy constructor and the assignment operator are both made private
and not implemented - if the class is not copyable then noone should copy.
Is there a case when copy constructor and assignment operator need to be private
and have meaningful implementation at the same time?
There are two cases that come to mind immediately:
friend
s:
Say that, as part of your design, you have two highly coupled classes where one needs to be able to copy the other (say, as in a factory model or some such), but you don't want to let the whole world be able to copy it.
wrappers:
Say you want to be able to conditionally clone some element, depending on some internal behavior (e.g., depending on some class-stateful condition) - the cleanest way, from a language perspective - is still to separate the copying into its own function. This would allow for good separation of concerns.