How to utilize a class object in parameters of an overloaded operator?
Oct 22, 2018 at 8:23pm UTC
I've posted a sample of my code, in which I want to add the length of the rectangle passed in parameters to the current object's length, and width respectively. How would I do that though?
1 2 3 4 5
rectangleType rectangleType::operator +(const rectangleType& rectangle) const
{
this ->length += rectangle.length;
this ->width += rectangle.width;
}
Last edited on Oct 22, 2018 at 8:29pm UTC
Oct 22, 2018 at 8:44pm UTC
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12
rectangleType &rectangleType::operator +=(rectangleType const & rhs)
{
this ->length += rhs.length;
this ->width += rhs.width;
return *this ;
}
// NOTE: non-member function
rectangleType operator +(rectangleType lhs, rectangleType const &rhs)
{
return lhs += rhs;
}
Topic archived. No new replies allowed.