Why assignment operator returns ref not pointer?

There's nothing preventing you from returning a pointer if you'd like (though, I don't see why), but if you want to mimick the behaviour of the integral types in c++ you return a reference.

Alright, not convincing, but I take it. – Alcott Aug 23 at 10:56 1 @Alcott: The language tries to make user defined types as similar to fundamental types as possible, and this is just one more example. If you are allowed to do I = j = k with any fundamental type, you should also be able to do it with a user defined type that overloads operator=.

Returning a pointer would just make things messier and inconsistent: I = *(j = k) would be weird to parse... – David Rodríguez - dribeas Aug 23 at 11:17 ya, convincing enough, thanks man. – Alcott Aug 23 at 11:18.

Returning a reference allows you to say x = y = z; which a pointer wouldn't, and generally lets you use x = y as an lvalue of the same type as x. You're free to overload any assignment operator you like, but the standard practice of returning a reference to the object itself is very useful.

Returning a pointer would mean that instead of writing a = be = c; you would have to write *(a = b) = c; which is ugly and also contrdicts normal C/C++ usage.

– jamesdlin Aug 23 at 11:15 1 That would be a = *(b = c); to have the same effect. Which strengthens your argument - it's not just ugly and unexpected, it's also easier to use incorrectly. – Mike Seymour Aug 23 at 11:15.

The assignment operator sets the operators left hand side equal to its right hand side, so conceptually the types of lhs and rhs should match. If your assignment operator takes a reference as its parameter, it ought to return a reference: Foo &operator=(const Foo &f); It would be downright weird to write: Foo *operator=(const Foo &f); // this is weird because type of lhs! = type of rhs Now, you could provide a version of operator=() that assigns one Foo* to another: Foo *operator=(const Foo *f); but the "big three" or "rule of three" doesn't say anything about assigning pointers.

The "rule" says that if you override any of {destructor, assignment operator, copy constructor}, you'll probably need to override all of them because they should all deal with the same set of ivars. But that's when you're assigning one object to another. Assigning one pointer to another is usually the same no matter the type of the pointer.

Think what would happen if you used the Foo* version of the assignment operator in place of the reference version: you'd have no way to set two pointers to point to the same object.

Thanks for your detailed answer. – Alcott Aug 23 at 11:52.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions