r/learncpp • • Jan 05 '19

Confused about a constructor call

This code:

#include <iostream>
using namespace std;
class A{
public:
    A(){f();}
    virtual void f() const {cout << "A :: f() " << endl;}
};
class B : public A{
public:
    virtual void f() const {cout << "B :: f() " << endl;}
};
int main(){
    B b;
    return 0;
}

outputs:

A :: f() 

I understand B's constructor calls A's constructor, which calls f, but shouldn't it call B::f ? as f is a virtual function.

Thanks in advance for your help

1 Upvotes

2 comments sorted by