- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
若C c1; 则c1.a和c1.A::a有二义性,此二义性由共同的基类产生。 解决方法:c1.B1::a或c1.B2::a或 int C::f( ){ return B1::a+B2::a; } 二义性例子2: class A class B1:public A { public : int a; }; { private: int b1; }; class B2:public A class C:public B1,public B2 { private: int b2; }; { public: int f( ); private: int c; }; 二义性例子2: class A class B1:public A { public : int a; }; { private: int b1; }; class B2:public A class C:public B1,public B2 { private: int b2; }; { public: int f( ); private: int c; }; 二、虚基类 虚基类用于解决有共同基类的二义性问题 声明: 以virtual修饰说明基类例:class B1:virtual public B 作用: 1、主要用来解决多继承时可能发生的对同一基类继承多次而产生的二义性问题。 2、为最远的派生类提供唯一的基类成员,而不重复产生多次拷贝。 注意: 在第一级继承时就要将共同基类设计为虚基类。 若D d; 则 d.f( ) 是正确的 class B:virtual public A { protected: int b; }; class C:virtual public A { protected: int c; }; class D:public B, public C { public int g( ); private: int d; }; 举例: class A { public: void f( ); protected: int a; }; #include iostream using namespace std; class B0 { public: B0(int n){nv=n;coutmember of B0endl;} int nv; void fun(){coutfun of BOendl;} }; class B1:virtual public B0 { public: B1(int a):B0(a){coutmember of B1endl;} int nv1; }; class B2:virtual public B0 { public: B2(int a):B0(a){coutmember of B2endl;} int nv2; }; class D1:public B1,public B2 { public: D1(int a):B0(a),B1(a),B2(a) {coutconstructing D1endl;} int nvd; void fun(){coutfun of D1 nvendl;} }; void main() { D1 d1(1); d1.nv=2; d1.fun(); } 运行结果 m
您可能关注的文档
最近下载
- 汉语语法教学-越来越……/越……越…….ppt VIP
- (5)显卡 836348006V323(msi GTX980TI MS-V323 图纸和点位图).pdf VIP
- RJ八上Unit 6 Lesson 4 2025-2026学年人教版英语八年级上册.pptx
- RJ八上Unit 6 Lesson 3 2025-2026学年人教版英语八年级上册.pptx
- RJ八上Unit 6 Lesson 1 2025-2026学年人教版英语八年级上册.pptx
- RJ八上Unit 6 Lesson 2 2025-2026学年人教版英语八年级上册.pptx
- 2023款比亚迪宋PLUS DM-i 冠军版_汽车使用手册用户操作图示图解详解驾驶指南车主车辆说明书电子版.pdf
- 蓝牌随车吊的型号及报价.docx VIP
- 汉责文化网站.pptx VIP
- 全国职业院校技能大赛 赛题汇总 2023高职组 GZ054 数字艺术设计赛题第01--10套.docx
文档评论(0)