Practice mode C++

What does a conventional copy assignment operator for class X return?

Correct answer A. A reference to *this, of type X&

Explanation

Returning X& (that is, *this) lets assignments chain, as in a = b = c, and matches the behaviour of built-in types. Returning void would break chaining, while returning by value would create unnecessary copies. The compiler-generated copy and move assignment operators also return X&, so user-defined versions should follow the same convention.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.