http://www.azure.com.cn/article.asp?id=342
看下面这个例子:
class CBase
{
public:
virtual void Test(int iTest = 0) const = 0;
};
class CDerived : public CBase
{
public:
void Test(int iTest = 1) const { cout << iTest << endl; };
};
{
public:
virtual void Test(int iTest = 0) const = 0;
};
class CDerived : public CBase
{
public:
void Test(int iTest = 1) const { cout << iTest << endl; };
};
下面是调用例子:
CBase *p = new CDerived;
p->Test();
p->Test();
我们的本意是想输出1,可是结果却是输出0!
不用惊奇,书中给出了答案:虚拟函数系动态绑定(dynamically bound),而缺省参数值却是静态绑定(statically )所以,<<Effective C++>>第38条告诉我们: 绝对不要重新定义继承而来的缺省参数。