mardi 2 février 2016

About virtual function and inheritance

About the following code:

class A {
    A * next;
    static A* tmp;
public:
    A() : next(tmp) {
        if (tmp)
            tmp->next = this;
        tmp = this;

    }
    virtual void print() {
        if (next)
            next->print();
    }
};
class B : public A {
    int a = 1;
public:
    void print() {
        cout << "foo";
        A::print();
    }
};
A* A::tmp;
int main(){
    B b;
    b.print();
}

Why does next->print(); leads to B::print() and not back to A::print()? Since next is a static pointer of A why does it go to B's function?

Aucun commentaire:

Enregistrer un commentaire