I am currently studying "Object-oriented design choices" by Dingle (2021), a required textbook for my Object-Oriented Design Class. While going through the chapter on "Move Semantics," I encountered an example discussing deep and shallow copying, which struck me as unusual.
The first half of this chapter delves into copy semantics, specifically the Copy Constructor and the Overloaded Assignment Operator.
The example in question, provided in the book and simplified for clarity, is as follows:
class goodMM {
private:
int* m_heapData;
int m_size;
void copyData(const goodMM& other) {
// ...
}
public:
goodMM(int size) {
// ...
}
~goodMM() {
// ...
}
goodMM(const goodMM& other) {
// ...
}
void operator=(const goodMM& other) {
// ...
}
}
The part that sticks out to me is the operator definition. I was taught that the operator should always return a reference & to allow for:
- Chaining assignment
a = b = c - Efficiency (avoids copying which can be expensive)
I tried to find something related to this topic on google and the SO search engine, but the results yield the void*() operator. Checked the cppreference as well and they all return a reference of type T [T&].
Is there any specific benefit or advantage to declaring the operator= as void in this context, as demonstrated in the example from the book?