Effective C++ 学习笔记(23)
2011-08-07 13:40 Daniel Zheng 阅读(207) 评论(0) 编辑 收藏 举报决不要重新定义继承而来的缺省参数值
#include <iostream>
using namespace std;
enum color
{
RED,GREEN,BLACK,WHITE
};
class Base
{
public:
virtual void ShowColor(color myColor = BLACK)
{
cout<<"Base Color is "<<myColor<<endl;
}
};
class Derived:public Base
{
public:
virtual void ShowColor(color myColor = RED)
{
cout<<"Derived Color is "<<myColor<<endl;
}
};
int main()
{
Base * B = new Derived;
B->ShowColor();
return 0;
}
运行结果:
Derived Color is 2
原因:
虚函数是动态绑定而缺省参数值是静态绑定的。