class Base {
public:
virtual void f(float) { cout << "Base::f(float)\n"; }
};
class Derived : public Base {
public:
virtual void f(int) { cout << "Derived::f(int)\n"; }
};
int main() {
Derived *d = new Derived();
Base *b = d;
b->f(3.14F);
d->f(3.14F);
}
-C++ does not support contravaraint return type, so the f(int) does not override f(float)
-Polymorphism is supported by C++, so both d and b should point to vtable for the derived class.
-vtable for derived class is something like 0: f(float), 1: f(int) etc.
My answer for the question is Base::f(float) is called twice, but the answer is:
Base::f(float) Derived::f(int)
Why is this the case? Does accessing derived class from different pointer enforce certain rules? Far as I am aware, object slicing only occurs when using copy-ctor or copy-assignment, with pointers, they should all point to the same vtable.