Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 349
sree Avatar asked Apr 17 '26 06:04

sree


1 Answers

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.

like image 135
Aesthete Avatar answered Apr 18 '26 18:04

Aesthete



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!