How function chaining is working without T& as return type of operator + and = | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How function chaining is working without T& as return type of operator + and =

Please refer code below: It works fine but I am concerned about how it is working ok without complex& for operator + As this would be calling unnecessary copy constructor twice for c123, how to do complex& for operator +? https://code.sololearn.com/cLmSIBW4NoFS/?ref=app P.s. : + should not be considered as complex& and I got that point.... Why = also working without complex& operator= for c12 = c123 = c2?

28th Mar 2022, 2:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 1
operator+ cannot have complex& as the return type for an obvious reason(you shouldn't return an lvalue reference to a local variable) For operator=, no. The copy constructor won't be called twice because of return value optimization(RVO). Search for it on the web for more details.
2nd Apr 2022, 4:01 PM
Anthony Maina
Anthony Maina - avatar