You've answered 1 of 189 questions correctly. (Clear)
Question #160 Difficulty:
According to the C++23 standard, what is the output of this program?
#include <iostream>
struct A {
virtual void foo (int a = 1) {
std::cout << "A" << a;
}
};
struct B : A {
virtual void foo (int a = 2) {
std::cout << "B" << a;
}
};
int main () {
A *b = new B;
b->foo();
}
Correct!
In the first line of main, we create a new B object, with an A pointer b pointing to it.
On the next line, we call b->foo(), where b has the static type A, and the dynamic type B. Since foo() is virtual, the dynamic type of b is used to ensure B::foo() gets called rather than A::foo().
However, which default argument is used for the int a parameter to foo()? §[dcl.fct.default]¶10 in the standard:
A virtual function call (§[class.virtual]) uses the default arguments in the declaration of the virtual function determined by the static type of the pointer or reference denoting the object. An overriding function in a derived class does not acquire default arguments from the function it overrides.
So B::foo() is called, but with the default argument from A::foo(), and the output is B1.
You can explore this question further on C++ Insights or Compiler Explorer!
Mode : Training
You are currently in training mode, answering random questions. Why not Start a new quiz? Then you can boast about your score, and invite your friends.
Contribute
C++ Brain Teasers
Get the book, support the site!