I know my copy constructors and overloaded operators are rather arbitrary in this case, but I looking for someone to verify if i'm making them correctly, as well as using *this correctly.
Also, could someone tell me why the copy constructor is called every time I return *this or an object of type Rectangle from any of my overloaded operators?
class Rectangle
{
private:
int length;
int width;
static int counter;
public:
Rectangle(int len = 0, int w = 0)
{
length = len;
width = w;
counter++;
}
~Rectangle()
{ counter--; }
static int getCounter()
{ return counter; }
Rectangle(const Rectangle &);
Rectangle operator+ (const Rectangle &);
Rectangle operator= (const Rectangle &);
};
int Rectangle::counter = 0;
Rectangle::Rectangle(const Rectangle &right) : Rectangle()
{
width = right.width;
length = right.length;
}
Rectangle Rectangle::operator+ (const Rectangle &right)
{
width += right.width;
length += right.length;
return *this;
}
Rectangle Rectangle::operator= (const Rectangle &right)
{
width = right.width;
length = right.length;
return *this;
}