Mastering C++: A Comprehensive Quiz Based on 'Thinking in C++'

Disable ads (and more) with a membership for a one time $2.99 payment

Test your C++ skills with our quiz based on Bruce Eckel's 'Thinking in C++'. Dive into object-oriented programming, advanced topics, and fundamentals. Perfect for learners and experts alike. Assess your knowledge and become a C++ master!

Practice this question and more.


What is the purpose of returning by value as a const in operator overloading?

  1. To modify the operand

  2. To avoid modifications on temporary objects

  3. To allow changes on the returned object

  4. To enforce type safety

The correct answer is: To avoid modifications on temporary objects

When overloading an operator, returning by value as a const ensures that the returned object cannot be modified. This is done to avoid any potential modifications on temporary objects, which can lead to unexpected behavior in the code. Option A is incorrect because operator overloading should not modify the operands, but rather perform an operation on them. Option C is incorrect because using const indicates that the returned object should not be modified. Option D is incorrect because enforcing type safety is not the primary purpose of returning by value as a const in operator overloading.