标签:style blog http color 使用 for
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
? 不懂 c++为什么derived classes 内的名称要遮掩 base classes 内的名称。
经验:derived classes 内的名称会遮掩 base classes 内的名称。在 public 继承下从来没有人希望如此。class Base { private: int x; public: virtual void mf1() = 0; virtual void mf1(int); virtual void mf2(); void mf3(); void mf3(double); //... }; class Derived: public Base{ virtual void mf1() {}; void mf3(); void mf4(); //... }; Derived d; int x; d.mf1(); //ok. 调用 Derived::mf1 d.mf1(x); //error. 因为Derived::mf1遮掩了 Base::mf1 d.mf2(); //ok. 调用 Base::mf2 d.mf3(); //ok. 调用 Derived::mf3 d.mf3(x); //error. 因为Derived::mf3遮掩了 Base::mf3
class Base{ private: int x; public: virtual void mf1() = 0; virtual void mf1(int); virtual void mf2(); void mf3(); void mf3(double); //... }; class Derived: public Base{ public: using Base::mf1; //让 Base class 内名为mf1和mf3的所有东西在 Derived 作用域内都可见(并且 public) using Base::mf3; virtual void mf1(); void mf3(); void mf4(); //... }; Derived d; int x; d.mf1(); //ok. 调用 Derived::mf1 d.mf1(x); //ok. Base::mf1 d.mf2(); //ok. 调用 Base::mf2 d.mf3(); //ok. 调用 Derived::mf3 d.mf3(x); //ok. Base::mf3
class Base{ private: int x; public: virtual void mf1() = 0; virtual void mf1(int); //... 与前同 }; class Derived: private Base{ virtual void mf1() //转交函数 {Base::mf1();} }; Derived d; int x; d.mf1(); //ok. 调用的是 Derived::mf1 d.mf1(x);//error. Base::mf1()被遮掩了
Effective C++ Item 33 避免遮掩继承过来的名称,布布扣,bubuko.com
Effective C++ Item 33 避免遮掩继承过来的名称
标签:style blog http color 使用 for
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37689999