samedi 2 mars 2019

pure virtual function with implementation in C++

I'm aware that one can make implementation for pure virtual function in base class, as a default implementation. But i don't quite understand the code below.

class A {
    virtual void f1() = 0;
    virtual void f2() = 0;
};
void f1(){
    cout << "base f1" << endl;
    f2();
}
void f2(){
    cout << "base f2" << endl;
}

class B: public A {
    void f1() { A::f1(); }
    void f2() { cout << "derived f2" << endl; }
};

int main(){
    B b;
    b.f1();
}

Why does the B::f1() calls B::f2() instead of A::f2. I know it will behave this way, but why? what basic knowledge i have missed.

Aucun commentaire:

Enregistrer un commentaire