//////////////////////////////////////////////////////////////////////////
// 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,而不是Green
pRectangle->draw();
}
return 0;
}
绝不重新定义继承而来的缺省参数值
最新推荐文章于 2025-08-01 10:01:45 发布