Call virtual method from base class on object of derived type

Posted by Nikola Smiljanic on Stack Overflow See other posts from Stack Overflow or by Nikola Smiljanic
Published on 2010-05-07T10:14:17Z Indexed on 2010/05/07 10:18 UTC
Read the original article Hit count: 314

Filed under:
class Base
{
public:
    virtual void foo() const
    {
        std::cout << "Base";
    }
};

class Derived : public Base
{
public:
    virtual void foo() const
    {
        std::cout << "Derived";
    }
};

Derived d; // call Base::foo on this object

Tried casting and function pointers but I couldn't do it. Is it possible to defeat virtual mechanism (only wondering if it's possible)?

© Stack Overflow or respective owner

Related posts about c++