0

Hi,

I have overloaded assignment operator like below and it worked perfectly fine.

void Test::operator = (Test& obj)
{
    this->a = obj.a;
    this->b = obj.b;
}

But when I googled more about this, I could see all of the examples used a different prototype like this

Test& Test::operator = (Test& obj)
{
    //do something on *this
    return *this;
}

Is there anything wrong in the first method?

3
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by subith86
0

Yes, I'm surprised your compiler allowed it. The assignment operator must return a reference to the object.

1

How, with the first method, would you expect to support something like a = b = c; ?

0

How, with the first method, would you expect to support something like a = b = c; ?

satisfying answer. thank you :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.