避免遮掩继承而来的名称 实验示例
来源:互联网 发布:sql 身份证 年龄 编辑:程序博客网 时间:2024/04/28 12:33
只要成员名称一样(即使base classes和derived classes内的函数有不同的参数类型,而且不论函数是vitual或non-virtual),继承类中的名称就会遮掩base class内的名称。
表现如下:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void mf1()
- {cout<<"Base::mf1"<<endl;}
- void mf1(int a)
- {cout<<"Base::mf1(int)"<<endl;}
- void mf2()
- {cout<<"Base::mf2"<<endl;}
- };
- class Derived:public Base
- {
- public:
- void mf1()
- {cout<<"Derived::mf1"<<endl;}
- void mf2()
- {cout<<"Derived::mf2"<<endl;}
- void CallBase()
- {Base::mf1();}
- };
- int main()
- {
- Derived d;
- d.mf1();
- d.CallBase();//正确。如果在类体内通过Base::mf1();则可调用父类的同名函数。
- d.mf1(1);//报错 no matching function for call to `Derived::mf1(int)'
- //candidates are: void Derived::mf1()
- getchar();
- }
可以Derived类体中直接通过其它函数调用Base中没有被同名遮掩的成员函数,或者通过Derived对象都可以直接调用Base中没有被同名遮掩的成员函数。(前提是满足public\protected\private的继承约束)。例:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void init()
- {cout<<"Base::init"<<endl;}
- void mf1(int a)
- {cout<<"Base::mf1(int)"<<endl;}
- void mf2()
- {cout<<"Base::mf2"<<endl;}
- };
- class Derived:public Base
- {
- public:
- void CallBase()
- {init();}
- };
- int main()
- {
- Derived d;
- d.CallBase();//正确
- d.init();//正确
- getchar();
- }
一个virtual函数的例子:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void init()
- {mf1();}
- private:
- virtual void mf1()
- {cout<<"Base::mf1"<<endl;}
- };
- class Derived:public Base
- {
- private:
- virtual void mf1()
- {cout<<"Derived::mf1"<<endl;}
- };
- int main()
- {
- Derived d;
- d.init();//正确
- Base b;
- b.init();
- Base &tb=d;
- tb.init();
- getchar();
- }
通过基类的init()访问不同类型的mf1(),实现多态。
如果private继承则:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void init()
- {mf1();}
- private:
- virtual void mf1()
- {cout<<"Base::mf1"<<endl;}
- };
- class Derived:private Base
- {
- private:
- virtual void mf1()
- {cout<<"Derived::mf1"<<endl;}
- };
- int main()
- {
- Derived d;
- // d.init();//报错:`void Base::init()' is inaccessible
- //`Base' is not an accessible base of `Derived'
- Base b;//正确
- b.init();//正确
- // Base &tb=d;//报错 `Base' is not an accessible base of `Derived'
- // tb.init();
- getchar();
- }
上面代码再变化,把d存到Base b中,造成切割后:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void init()
- {mf1();}
- private:
- virtual void mf1()
- {cout<<"Base::mf1"<<endl;}
- };
- class Derived:public Base
- {
- private:
- virtual void mf1()
- {cout<<"Derived::mf1"<<endl;}
- };
- int main()
- {
- Derived d;
- Base b=d;//Derived部分被切割掉了,结果只剩下基类的mf1可供选择了。
- b.init();
- getchar();
- }
通过using声明式让被遮掩的成员函数重见天日:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void mf1()
- {cout<<"Base::mf1"<<endl;}
- void mf1(int x)
- {cout<<"Base::mf1(int)"<<endl;}
- };
- class Derived:public Base
- {
- public:
- using Base::mf1;//这里用了using 声明式
- void mf1()
- {cout<<"Derived::mf1"<<endl;}
- };
- int main()
- {
- Derived d;
- d.mf1();
- d.mf1(1);//有了using Base::mf1 就Derived对象就可以访问基类中被遮掩的同名函数了
- getchar();
- }
通过using声明式,在Derived类中引入基类被隐藏的函数。
通过转交函数让基类中的函数重见天日:
- #include<iostream>
- using namespace std;
- class Base
- {
- public:
- void mf1()
- {cout<<"Base::mf1"<<endl;}
- void mf1(int x)
- {cout<<"Base::mf1(int)"<<endl;}
- };
- class Derived:public Base
- {
- public:
- void mf1()
- {Base::mf1();} //转交
- };
- int main()
- {
- Derived d;
- d.mf1();
- getchar();
- }
- 避免遮掩继承而来的名称 实验示例
- Effective C++条款33:避免遮掩继承而来的名称 实验示例
- 避免遮掩继承而来的名称
- 避免遮掩继承而来的名称
- 避免遮掩继承而来的名称
- 继承:避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- 条款33----避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- item33: 避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- Effective C++ 33 避免遮掩继承而来的名称
- 条款33、避免遮掩继承而来的名称
- 条款33:避免遮掩继承而来的名称
- MySQL编译安装
- C++ 虚函数表解析
- PCB routing
- 求解答,我的截图中咋这样,哪里出错了
- Flex和AIR中的垃圾回收实践之对象缓存[译文]
- 避免遮掩继承而来的名称 实验示例
- “丈母娘团”软件园找女婿:单身IT男成香饽饽
- super和this的用法
- 程序员是怎么学习的
- ORA-01855 :要求AM/A.M. 或 PM/P.M.
- javaweb 路径
- C++中的static关键字
- 独自徘徊的伤感日志:如果有一天,我从你的世界消失了
- C语言逻辑操作符的巧妙用法:an anagram of a palindrome