ProgrammingModerateProduct-company placement · C++

Why does a C++ copy assignment operator conventionally return T& (that is, *this)?

  1. A. Because returning void would make the assignment fail to compile
  2. B. Because returning by value would call the destructor of the left operand
  3. C. So that assignments can be chained like a = b = c without extra copies, as with built-in types
  4. D. Because the compiler requires every operator to return a reference

Correct answer: C. So that assignments can be chained like a = b = c without extra copies, as with built-in types

operator= may return anything, but returning a reference to the left operand mirrors built-in assignment, enables chaining and avoids making a needless copy.

More OOP with C++ and Java questions

Exams that ask this topic