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

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

Master C++ with our comprehensive quiz based on 'Thinking in C++'. Test your knowledge through flashcards and multiple-choice questions to enhance your understanding of C++. Prepare effectively for your exam!

Each practice test/flash card set has 50 randomly selected questions from a bank of over 500. You'll get a new set of questions each time!

Practice this question and more.


What is the behavior of a const reference in C++ when binding to a temporary object?

  1. It extends the lifetime of the temporary object to the lifetime of the reference

  2. It creates a copy of the temporary on the stack

  3. It immediately destroys the temporary object

  4. It converts the temporary into a dynamic object

The correct answer is: It extends the lifetime of the temporary object to the lifetime of the reference

A const reference in C++ extends the lifetime of the temporary object it is bound to, ensuring that the object remains valid for the duration of the reference. The other options are incorrect because creating a copy on the stack would not maintain the original object, immediately destroying the temporary is not the behavior of a reference, and converting the temporary into a dynamic object would not extend its lifetime to the reference.