using namespace std;
class BaseClass
{
public:
BaseClass(){};
~BaseClass(){};
virtual void testA()
{
cout << __FILE__ << "::" << __LINE__ << endl; // 2 called
testB();
}
virtual void testB()
{
cout << __FILE__ << "::" << __LINE__ << endl; // without testB() in MyClass, it will be called.
}
};
class MyClass: BaseClass
{
public:
MyClass(){};
~MyClass(){};
void testA()
{
cout << __FILE__ << "::" << __LINE__ << endl; // 1 called
BaseClass::testA();
}
void testB()
{
cout << __FILE__ << "::" << __LINE__ << endl; // 3 called
}
};
int main(int argc, char **argv)
{
MyClass myA;
myA.testA();
return 0;
}
Result:
VirtualFunction.cpp::31
VirtualFunction.cpp::12
VirtualFunction.cpp::37
沒有留言:
張貼留言