Suppose I have a class that doesn’t support memberwise copying so I don’t want to preserve compiler-implemented copy-constructor and assignment operator. I also don’t want to implement those because either
- doing so takes extra effort and I don’t need those operations in my class or
- those operations won’t make sense in my class
so I want to prohibit them. To do so I’ll declare them private and provide no implementation:
class NonCopyable {
private:
NonCopyable( const NonCopyable& ); //not implemented anywhere
void operator=( const NonCopyable& ); //not implemented anywhere
};
Now I can select any return type for operator=() member function. Will it matter which return type I select?
It matters a tiny, tiny bit:
voidensures a small percentage of accidental/misguided calls (a = b = c / f(d = e)) from within the class’s implementation produce compile time errors rather than link time errors, which may save compile time and be more understandable (minimally relevant for large classes touched by many developers, some with limited prior familiarity).voidwould ring an alarm bell for me (and hopefully most developers), wondering whether you:operator=operator=.operator=could have the opposite effect – all in the eye (and mind) of the beholder….