<<< Another issue with dynamic memory: assignment | Index | C++ references >>> |
How do we fix the problem with assignment?
We must provide our own version of the assignment operator to prevent Rectangle from making shallow assignment:
// C++ overloaded assignment operator: Rectangle& operator=( Rectangle const& another_rectangle_ ) { if ( this == &another_rectangle_ ) { // avoid assignment to self: return *this; } width = another_rectangle_.width; height = another_rectangle_.height; set_name( another_rectangle_.ptr_name ); return *this; }
<<< Another issue with dynamic memory: assignment | Index | C++ references >>> |