⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_5_repeated_inheritance_ambiguity.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//***************************************************
// 多继承的特例的重复继承下的二义性问题
//***************************************************

# include <iostream.h>

class A  //公共间接基类
{
public:
	int a;
};

class B1:public A  //直接基类
{
public:
	int b1;
};

class B2:public A  //直接基类
{
public:
	int b2;
};

class C:public B1,public B2  //重复继承派生类:有公共基类A数据成员a的2个副本
{
public:
	int c;
};

void main()
{
	C cc; //调用默认构造函数创建派生类对象

//无法对重复继承的公共间接基类的数据成员进行访问赋值
//	cc.a=100; //Error: 'C::a' is ambiguous //多继承路经产生二义性错误!
	          //Warning: could be the 'a' in base 'A' of base 'B1' of class 'C'
              //Warning: or the 'a' in base 'A' of base 'B2' of class 'C'

	cc.b1=200; //对直接派生类数据成员赋值
	cc.b2=300; //对直接派生类数据成员赋值

//	cc.c=cc.a+cc.b1+cc.b2; //Error: 'C::a' is ambiguous //多继承路经产生二义性错误!
	                       //Warning: could be the 'a' in base 'A' of base 'B1' of class 'C'
                           //Warning: or the 'a' in base 'A' of base 'B2' of class 'C'
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -