#include <iostream>
using namespace std;
//-------------------------------
class A1{
public:
int a;
public:
void m();
}; void A1::m() {
cout<<"A1::m():a="<<this->a<<endl;
} //-------------------------------
class A2 {
public:
int a;
void m();
}; void A2::m() {
cout<<"A2::m(),a="<<this->a<<endl;
} //-------------------------------
class B :public A1, public A2{
public:
void show();
}; void B::show()
{
cout<<"A1::a="<<this->A1::a<<endl;
cout<<"A2::a="<<this->A2::a<<endl;
} //-------------------------------
void f1() {
B b;
b.A1::a = ;
b.A2::a = ; b.A1::m();//这时不能用b.m(),具有歧义;
b.A2::m();//用格式 b.A1::m(), b.A2::m()明确对象,消除歧义
b.show();
}
int main() {
f1();
while();
return ;
}
/*测试结果:

A1::m():a=34
A2::m(),a=32432
A1::a=34
A2::a=32432

*/

上面是两个基类有同样名称和形式的函数,都被继承到了子类中。访问他们的时候,要加上作用域才能正确地访问。

进一步来看,如果两个类都从同一个类派生,并没有重写某些函数,再有一个子类继承了它们两个。[共同基类产生的二义性]

情况就和上面类似了。代码如下:

#include <iostream>
using namespace std;
#include <string> class A
{
public:
int m_ax;
void show(); A();
A(int a); };
A::A()
{ }
A::A(int val)
{
this->m_ax = val;
} void A::show()
{
cout << "A::m_ax = "<<m_ax<<endl;
}
class B1: public A{ };
class B2: public A{ };
class C: public B1 ,public B2 {
public:
int m_cx;
void show();
};
void C::show()
{
//cout<<"c::show: m_ax = "<<m_ax<<endl;// error C2385: 对“m_ax”的访问不明确
cout<<"c::show: A::m_ax = "<<A::m_ax<<endl;
cout<<"c::show: B::m_ax = "<<B1::m_ax<<endl;
cout<<"c::show: B::m_ax = "<<B2::m_ax<<endl;
//从A、B1、B2派生下来的函数以及变量,在类C里面都得以保存,并各自占各自的独立的空间。
//eg:尽管m_ax最初源于A类,但是派生到C类里面的有三个不同的m_ax。对于方法,同理。
}
int main()
{
C c1 ;
c1.show(); //c1.m_ax = 11;//error C2385: 对“m_ax”的访问不明确
c1.B1::m_ax = ;
c1.B1::show(); while();
return ;
}

专门解决共同基类产生的二义性的办法还有虚基派生

见另一篇:

  c++, 虚基派生 : 共同基类产生的二义性的解决办法
  http://www.cnblogs.com/mylinux/p/4096926.html

c++,多继承造成的二义性及解决办法的更多相关文章

  1. 【转】C++中多重继承的二义性及解决办法

    转自:http://www.cnblogs.com/lintong/archive/2012/07/28/2613002.html 1. 什么是多重继承的二义性 class A{ public: vo ...

  2. c++, 虚基派生 : 共同基类产生的二义性的解决办法

    虚基派生 //虚继承 #include <iostream> using namespace std; #include <string> //---------------- ...

  3. 关于android:windowNoTitle不起作用的解决办法

    今天测试一个新功能的时候,在styles.xml设置<item name="android:windowNoTitle">true</item>并没有生效, ...

  4. 继承在WCF中的问题和解决办法

    1. 问题 假设有代码如下: [ServiceContract] public interface IA { [OperationContract] string M1(); } [ServiceCo ...

  5. maven dependcymanage作用在父类里面定义依赖包 子类不会自动继承需要主动使用 这样解决了父类引用任意包 子类就会引用的问题

    maven dependcymanage作用在父类里面定义依赖包 子类不会自动继承需要主动使用 这样解决了父类引用任意包 子类就会引用的问题  子类在引用时候 不需要加上版本号

  6. C#使用ListView更新数据出现闪烁解决办法

    C#使用ListView更新数据出现闪烁解决办法 在使用vs自动控件ListView控件时候,更新里面的部分代码时候出现闪烁的情况 如图: 解决以后: 解决办法使用双缓冲:添加新类继承ListView ...

  7. Andriod学习笔记2:“Your content must have a ListView whose id attribute is 'android.R.id.list'”问题的解决办法

    问题描述 activity_main.xml代码如下: <?xml version="1.0" encoding="utf-8"?> <Lin ...

  8. Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...

  9. “maven编码gbk的不可映射字符”解决办法

    一.问题描述 Eclipse中使用Maven编译项目源代码时,如下的错误 java源代码在Eclipse中显示是没有任何错误的,可是执行"maven install"命令编译项目时 ...

随机推荐

  1. poj 1410 计算几何

    /** 注意: 千万得小心..就因为一个分号,调了一个晚上... **/ #include <iostream> #include <algorithm> using name ...

  2. windows server 2008/2012 无法安装AD域解决方法记录

    安装活动目录之前需要一些准备工作和前提条件的. 有管理员的权限 至少有一个足够大的NTFS文件系统的分区或动态卷 需要DNS服务配合,因此需要安装DNS服务 最好使用dcpromo命令来进行安装 最终 ...

  3. libcprops

    Install Howto Download the latest epel-release rpm from http://dl.fedoraproject.org/pub/epel/6/x86_6 ...

  4. Android平台APK分析工具包androguard的部署使用和原理分析

    原创文章,转载请注明出处,谢谢. Android应用程序分析主要有静态分析和动态分析两种,常见的静态分析工具是Apktool.dex2jar以及jdgui.今天突然主要到Google code上有个叫 ...

  5. 基于Visual C++2013拆解世界五百强面试题--题2-输出和为一个给定整数的所有组合

    请用C语言实现 输出和为一个给定整数的所有组合 启动2012 /* 请用C语言实现 输出和为一个给定整数的所有组合 */ #include <stdio.h> //包含头文件stdio.h ...

  6. java面试题系列11

    华为的JAVA面试题 QUESTION NO: 1 publicclass Test1 {       publicstaticvoid changeStr(String str){         ...

  7. Suricata, to 10Gbps and beyond(X86架构)

    Introduction Since the beginning of July 2012, OISF team is able to access to a server where one int ...

  8. Android 中文API (67) —— BluetoothClass.Device.Major

    前言 本章内容是android.bluetooth.BluetoothClass.Device.Major,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学 ...

  9. python下yield(生成器)

    python下的协程: #encoding=utf-8 """ 协程----微小的进程 yield生成器-----生成一个可迭代对象比如list, tuple,dir 1 ...

  10. Razor强类型视图下的文件上传

    域模型Users.cs using System;using System.Collections.Generic;using System.Linq;using System.Web; namesp ...