Base declares virtual void f(int x = 10); and Derived overrides it as void f(int x = 20);. Through a Base* b pointing at a Derived object, b->f() passes which argument?
AThe call is ambiguous and fails to compile, because the two declarations disagree about the default argument values used
B20, because the dynamic type of the object is Derived and its own default arguments apply to the call in full
C10, because default arguments are bound statically from the pointer's type while the override is chosen dynamicallyCorrect answer
D10 or 20 depending on the compiler, since the standard leaves the choice of default arguments unspecified here
Correct answer
C. 10, because default arguments are bound statically from the pointer's type while the override is chosen dynamically
Default arguments are not virtual: they are substituted at compile time from the static type of the expression, so Base's value 10 is used even though Derived::f executes. This mismatch is a classic trap, and the usual advice is to keep defaults identical across an override hierarchy or avoid them in virtual functions altogether. Dispatch of the function body itself still resolves at run time.