Search code examples
c++inheritancevirtualoptional-arguments

Overriding a virtual function with optional arguments


Why is this printing 23 as output; my expectation was 33. Could somebody please shed some light on this.

struct A {
    virtual void f() {cout << "1";}
};

/* Private inheritance */
struct B : private A {
    void f(int x = 0) {cout << "2";}
};

struct C : B {
    void f(){cout << "3";}
};

int main() {
    C obj;
    B &ref = obj;
    ref.f();
    obj.f();
}

Solution

  • The f(int x = 0) method in the B struct does not share a signature with either the A nor C struct's f() methods.