R@M3$H.NBlog

Call Base class method through base class pointer pointing to derived class

02 November, 2015 - 1 min read

class Base
{
public:
virtual void foo()
{}
};

class Derived: public Base
{
public:
virtual void foo()
{}
};

int main()
{
Base *pBase = NULL;
Base objBase;
Derived objDerived;

pBase = &objDerived;
pBase->foo();

/*Here Derived class foo will be called, but i want this to call
a base class foo. Is there any way for this to happen? i.e. through
casting or something? */
}

You can do it through scope resolution operator ::

Something like this:

pBase->Base::foo()