在继承一个virtual函数的时候,如果这个virtual函数同样是有默认值的话,那么其表明这次继承既存在动态绑定也存在静态绑定:例如下面这个例子:
1 class Shape{ 2 public: 3 enum shapeColor{red, green, blue}; 4 virtual void draw(shapeColor color = red)const = 0; 5 //注意,这里的颜色是静态绑定的 6 }; 7 class Recantagle : public Shape{ 8 public: 9 void draw(shapeColor color = green)const; 10 //但是这里的默认参数!!! 11 }; 12 class Circle : public Shape{ 13 public: 14 void draw(shapeColor color) const; 15 };
1 class Shape{ 2 public: 3 enum shapeColor{red, green, blue}; 4 void draw(shapeColor & color = red) const { 5 doDraw(color); 6 } 7 private://注意这里是private 8 virtual void doDraw(shapeColor & )const = 0; 9 }; 10 class Recantagle : public Shape{ 11 public: 12 //直接继承基类的public函数即可 13 private: 14 void doDraw(shapeColor & ) const; 15 };
原文:http://www.cnblogs.com/-wang-cheng/p/4889786.html