| <<< Returning *this | Index | Correct implementation for the Rectangle >>> |
An object that manages dynamic memory should also provide a move constructor:
// move constructor
Rectangle( Rectangle&& another_rectangle_ )
{
width = another_rectangle_.width;
height = another_rectangle_.height;
ptr_name = another_rectangle_.ptr_name;
another_rectangle_.ptr_name = nullptr;
}
The move constructor lets objects "move" from one scope to another without a potentially significant overhead of copying memory from a local object that is about to be destroyed:
Rectangle create_rectangle()
{
Rectangle temp;
temp.set_name( "FACTORY CREATED" );
return temp;
}
| <<< Returning *this | Index | Correct implementation for the Rectangle >>> |