C++ Quiz22 of 173
Difficulty:
⬅️ Prev: 27Quiz ID: 28Next: 29 ➡️

According to the C++23 standard, what is the output of this program?

#include <iostream>

struct A {
  A() { std::cout << "A"; }
  A(const A &a) { std::cout << "B"; }
  virtual void f() { std::cout << "C"; }
};

int main() {
  A a[2];
  for (auto x : a) {
    x.f();
  }
}
Hint

When are array elements initialized? What is the difference between auto and auto&?

Reveal answer

AABCBC

Explanation

When the array is initialized, the default constructor is called once for each of the two objects in it.

Then we iterate over the array using auto, which in our case is deduced to be A. This means the copy constructor will be called before f() for each iteration, printing BCBC. (Just as if we had written for (A x: a).)

If we want to avoid the copy constructor, we can write for (auto& x : a) instead. Then the loop would print CC. (Just as if we had written for (A& x: a).)

⬅️ Prev: 27Quiz ID: 28Next: 29 ➡️