ProgrammingHardMicrosoft/Amazon SDE MCQ · GATE CS

What is printed?

What is printed?
struct T { ~T() { cout << "d"; } };
T make() { return T(); }
int main() {
    const T& r = make();
    cout << "x";
}
  1. A. dx
  2. B. xd
  3. C. dxd
  4. D. x

Correct answer: B. xd

Binding the temporary returned by make() to a const reference extends its lifetime to that of r, so it is destroyed at the end of main, after x is printed. In C++17 no extra copies are made.

More OOP with C++ and Java questions

Exams that ask this topic