多重继承-二义性: 十分简单...
就是Derived类继承了两个类Base1,Base2的属性,但是Base1和Base2当中有相同的函数成员或者数据成员的时候,子类Derived调用的时候 将如何调用? 显然会产生 二义性...因此需要用作用域操作符::来帮忙.此外,如果Derived类当中也有相同名的成员函数或者数据成员,那么父类的相同名的成员会被本来的覆盖掉...
实例:
// 多重继承-二义性.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
class Base1
{
public:
int i;
Base1(int B1i):i(B1i)
{
}
void func()
{
cout << "Base1.func()" <<endl;
}
};
////
class Base2
{
public:
int i;
Base2(int B2i):i(B2i)
{
}
void func()
{
cout << "Base2.func()" <<endl;
}
};
////
class Derived:public Base1,public Base2
{
public:
void func()
{
cout << "Derived func()" <<endl;
}
void call_func()
{
cout << Base1::i <<endl;
cout << Base2::i <<endl;
Base1::func();
Base2::func();
func();
}
Derived(int i,int j):Base1(i),Base2(j)
{
}
};
////main()
int _tmain(int argc, _TCHAR* argv[])
{
Derived d(1,2);
d.call_func();
d.Base1::i = 100;
d.Base2::i = 199;
d.call_func();
getchar();
return 0;
}