Why does the compiler not throw error in main() for p->f4(); line.
As per my understanding class B hides f4() and should throw error
class A
{
public:
void f1(){}
virtual void f2(){}
virtual void f3(){}
virtual void f4(){}
};
class B : public A
{
public:
void f1(){}
void f2(){}
void f4(int x){}
};
int main()
{
A *p;
B o2;
p = &o2;
p->f1();
p->f2();
p->f3();
p->f4(); //why doesn't it throw an error for this line as class B hides f4()
p->f4(5);
return 0;
}
The static type of the pointer
pisA *. So for this callthe function
f4is searched in the scope of the classAand is found and called. As the function is declared as virtual then if it would be overridden in the classBthen the overriding function in the classBwould be called.But for this call
the compiler should issue an error because within the class
Athere is no function with the namef4that accepts an argument of the typeint.