"Derived * ptr" "ptr" -, Derived- . -, Derived - .
If you want to access the base class version of the function "f", use "Base * ptr" and it will automatically select the correct version of the function, as shown :)
class Base {
public:
virtual void f()
{
cout<<"Here 2"<<endl;
}
void f(int x)
{
cout<<"Here 1"<<endl;
}
virtual ~Base() {}
};
class Derived : public Base {
public:
void f()
{
cout<<"Here 3"<<endl;
}
virtual ~Derived() {}
};
int main()
{
Base *ptr = new Derived;
ptr->f(1);
delete ptr;
return 0;
}
conclusion here 1
source
share