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?
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.