孙鑫教程第三章小测试程序
#include <IOSTREAM.H> //定义一个基类,模拟CWinApp class Base { public: Base(); // virtual void fn();/*测试虚函数结果:call the Derived's fn*/ void fn();/*测试非虚函数结果:call the Base's fn*/ Base *p; }; Base::Base() { p = this;//this指针指向哪一个对象?答:指向派生类对象dd } void Base::fn() { cout << "call the Base's fn" << endl; } //定义一个派生类,模拟CTestApp class Derived:public Base { public: void fn(); }; void Derived::fn() { cout << "call the Derived's fn" << endl; } //定义一个派生类的全局对象,模拟theApp Derived dd; int main() { (dd.p)->fn(); return 0; }