Scuba Steve
Scuba Steve

Reputation: 1648

deep copy problems in C++

So I have a class Rectangle with an overloaded operator= defined as follows:

    Rectangle& Rectangle::operator=(Rectangle &rhs)
{
    if (this != &rhs)
    {
        m_x = rhs.m_x;
        m_y = rhs.m_y;
        m_width = rhs.m_width;
        m_height = rhs.m_height;
        m_intersection = rhs.m_intersection;
    }
    return *this;
}

The compiler doesn't complain so far. But when I attempt to do an assignment my compiler is complaining:

m_boundingBox = Rectangle(x, y, width, height);

Now I'm trying to avoid calling new, hence the assignment this way. I'm assuming I need to implement a separate copy constructor for this kind of assignment, but I'm at a loss as to what the signature should look like.

Here's the compiler error: Error (active) E0349 no operator "=" matches these operands

Firstly, I'd like to fix the above. But secondly, if you can shed some light on the entire subject for this confused code-writing ape, that'd be amazing. Teach a man to fish and all.

Upvotes: 0

Views: 230

Answers (1)

Jeremy Friesner
Jeremy Friesner

Reputation: 73081

Your problem is that you haven't declared the argument const. It should be this:

Rectangle& Rectangle::operator=(const Rectangle &rhs)

Upvotes: 2

Related Questions