1. 虚函数中存在默认值时,需要注意其函数调用中默认值:
class cbase { public: virtual void func(int a = 10) { cout << "this is cbase func:" << a << endl; } }; class csub : public cbase { public: void func(int a = 20) { cout << "this is csub func:" << a << endl; } };
cbase * cbToCb = new cbase; cbase * cbTocSub = new csub; csub * cSubTocSub = new csub;
cbToCb->func(); // "this is csub func:10"
cbTocSub->func(); // "this is csub func:10"
cSubTocSub->func(); // "this is csub func:20"