Question on Virtual Methods
Posted
by bobber205
on Stack Overflow
See other posts from Stack Overflow
or by bobber205
Published on 2010-05-05T21:05:50Z
Indexed on
2010/05/05
21:18 UTC
Read the original article
Hit count: 160
IF both methods are declared as virtual, shouldn't both instances of Method1() that are called be the derived class's Method1()?
I am seeing BASE then DERIVED called each time. I am doing some review for an interview and I want to make sure I have this straight. xD
class BaseClass
{
public:
virtual void Method1() { cout << "Method 1 BASE" << endl; }
};
class DerClass: public BaseClass
{
public:
virtual void Method1() { cout << "Method 1 DERVIED" << endl; }
};
DerClass myClass;
((BaseClass)myClass).Method1();
myClass.Method1();
Method 1 BASE
Method 1 DERVIED
© Stack Overflow or respective owner