Effective C++ 学习笔记(23)
2011-08-07 13:40
260 查看
决不要重新定义继承而来的缺省参数值
运行结果:
Derived Color is 2
原因:
虚函数是动态绑定而缺省参数值是静态绑定的。
#include <iostream> using namespace std; enum color { RED,GREEN,BLACK,WHITE }; class Base { public: virtual void ShowColor(color myColor = BLACK) { cout<<"Base Color is "<<myColor<<endl; } }; class Derived:public Base { public: virtual void ShowColor(color myColor = RED) { cout<<"Derived Color is "<<myColor<<endl; } }; int main() { Base * B = new Derived; B->ShowColor(); return 0; }
运行结果:
Derived Color is 2
原因:
虚函数是动态绑定而缺省参数值是静态绑定的。
相关文章推荐
- Effective C++ 学习笔记(十三)
- “Effective C++ Third Edition”学习笔记(一)
- Effective C++ 学习笔记(三)
- Effective c++ 学习笔记之条款一视C++为一个语言联邦
- Effective C++ 学习笔记:关于继承
- Effective C++ 学习笔记(10)
- More Effective C++ 学习笔记(2)
- Effective C++ 学习笔记11
- Effective c++ 学习笔记(四)
- EffectiveC++学习笔记-条款39|40
- Effective Java 学习笔记(23)
- Effective-C++学习笔记
- C++之Effective STL学习笔记Item7
- Effective C++ 学习笔记(13)
- Effective c++ 学习笔记 1
- Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝
- Effective C++ 学习笔记(22)
- Effective C++ 学习笔记(27)
- “Effective C++ Third Edition”学习笔记(三)
- Effective C++ 学习笔记[2]