Reputation: 26873
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)?
Upvotes: 8
Views: 1712
Reputation: 22624
To explicitly call the function foo()
defined in Base
, use:
d.Base::foo();
Upvotes: 24
Reputation: 185862
d.Base::foo();
Note that d.foo()
would call Derived::foo
regardless of whether foo
was virtual or not.
Upvotes: 6