C++ Quiz21 of 173
Difficulty:
⬅️ Prev: 26Quiz ID: 27Next: 28 ➡️

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

#include <iostream>

struct A {
  virtual std::ostream &put(std::ostream &o) const {
    return o << 'A';
  }
};

struct B : A {
  virtual std::ostream &put(std::ostream &o) const {
    return o << 'B';
  }
};

std::ostream &operator<<(std::ostream &o, const A &a) {
  return a.put(o);
}

int main() {
  B b;
  std::cout << b;
}
Hint

What is the dynamic type of a?

Reveal answer

B

Explanation

This is a way to get polymorphic behaviour for operator <<.

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