绝不重新定义继承而来的缺省参数值

来源:互联网 发布:淘宝店铺url链接怎么做 编辑:程序博客网 时间:2024/05/16 02:42
//////////////////////////////////////////////////////////////////////////// Effective C++. // Item 37: Never redefine a function's inherited default parameter value.// 绝不重新定义继承而来的缺省参数值class IShape{public:enum ShapeColor{ Red, Green, Blue };virtual void draw(ShapeColor color = Red) const = 0;};class CRectangle: public IShape{public:virtual void draw(ShapeColor color = Green) const{int a = 0;return;}};int main(){IShape* pRectangle = new CRectangle;if (pRectangle){// 因为默认参数是静态绑定,所以这里传到CRectangle::draw()里的参数是Red,而不是GreenpRectangle->draw();}return 0;}

原创粉丝点击