C++ Quiz8 of 173
Difficulty:
⬅️ Prev: 7Quiz ID: 8Next: 9 ➡️

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

#include <iostream>

class A {
public:
  virtual void f() { std::cout << "A"; }
};

class B : public A {
public:
  void f() { std::cout << "B"; }
};

void g(A a) { a.f(); }

int main() {
  B b;
  g(b);
}
Hint

What is the dynamic type of a in void g(A a)?

Reveal answer

A

Explanation

g(A a) takes an object of type A by value, not by reference or pointer. This means that A's copy constructor is called on the object passed to g() (no matter if the object we passed was of type B), and we get a brand new object of type A inside g(). This is commonly referred to as slicing.

⬅️ Prev: 7Quiz ID: 8Next: 9 ➡️