标签:style blog http color cti io
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验:绝对不要重新而来的缺省参数值,因为缺省参数值都是静态绑定,而 virtual 函数 -- 你唯一应该覆写的东西 -- 却是动态绑定
示例:class Shape{ public: enum ShapeColor {Red, Green, Blue}; virtual void draw(ShapeColor color = Red) const = 0; }; class Rectangle: public Shape{ public: virtual void draw(ShapeColor color = Green) const; // 赋予不同缺省参数值 } class Circle: public Shape{ public: virtual void draw(ShapeColor color) const; //以对象调用此函数,一定要指定参数值 //以指针(reference)调用此函数,可以不指定参数值,从base 继承缺省参数值 }; //以下三个变量的静态类型都是 Shape * Shape *ps; Shape *pc = new Circle; Shape *pr = new Rectangle; pr->draw();
class Shape{ public: enum ShapeColor {Red, Green, Blue}; void draw(ShapeColor color = Red) const{ doDraw(color); } private: virtual void doDraw(ShapeColor color) const = 0;// }; class Rectangle: public Shape{ public: ... private: virtual void draw(ShapeColor color) const; // 赋予不同缺省参数值 }
Effective C++ Item 37 绝不重新定义继承而来的缺省参数值,布布扣,bubuko.com
Effective C++ Item 37 绝不重新定义继承而来的缺省参数值
标签:style blog http color cti io
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37724013