I have the following classes:
class A {
public:
virtual void f() {}
};
class B : public A{
public:
void f(int x) {}
};
If I say
B *b = new B();
b->f();
the compiler says error C2660: ‘B::f’ : function does not take 0 arguments.
Shouldn’t the function in B overload it, since it is a virtual function? Do virtual functions get hidden like this?
EDIT: I indeed meant to inherit B from A, which shows the same behaviour.
Assuming you intended
Bto derive fromA:f(int)andf()are different signatures, hence different functions.You can override a virtual function with a function that has a compatible signature, which means either an identical signature, or one in which the return type is “more specific” (this is covariance).
Otherwise, your derived class function hides the virtual function, just like any other case where a derived class declares functions with the same name as base class functions. You can put
using A::f;in class B to unhide the nameAlternatively you can call it as
(static_cast<A*>(b))->f();, or asb->A::f();. The difference is that ifBactually does overridef(), then the former calls the override, whereas the latter calls the function inAregardless.