虚函数缺省实参
Class Base { public: virtual void foo( int ival = 1024 ) { cout << "Base::foo() -- ival: " << ival << endl; } }; class Derived : public Base { public: virtual void foo( int ival = 2048 ) { cout << "Derived::foo() -- ival: " << ival << endl; } }; int main() { Derived *pd = new Derived; Base *pb = pd; pb->foo(); pd->foo(); delete pd; pb = NULL; return 0; }
虚函数的缺省实参不是在运行时刻决定的,而是在编译时刻根据被调用函数的对象的类型决定的!
上述输出为:
Derived::foo() -- ival: 1024
Derived::foo() -- ival: 2048