This article is Senlie original, reprint please retain this address:Http://blog.csdn.net/zhengsenlie
Experience: Never again the default number of parameters. Because the default values are static bindings, and the virtual function--the only thing you should overwrite--is dynamically bound
Demo Sample:
Analytical:
Because the PR is a Shape *, the default number of references is shape::red. Because the PR points to Rectangle, the call is Rectangle::d Raw
Although this is a C + + content, only can be included in the "Python Source code anatomy", Python's default parameters are placed in a static object pycodeobject at compile time
Correction: An alternative design using virtual functions. such as NVI: Make a public non-virtual function inside the base class call the private virtual function
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; assigning different default values}
Effective C + + Item 37 never again defines inherited default values