多重继承的问题三:
多重继承可能产生多个虚函数表,
derived-->baseA,baseB (都有虚函数) derived->VTableA,VTableB
#include <iostream>
#include <string>
using namespace std;
class BaseA
{
public:
virtual void funcA()
{
cout << "BaseA::funcA()" << endl;
}
};
class BaseB
{
public:
virtual void funcB()
{
cout << "BaseB::funcB()" << endl;
}
};
class Derived : public BaseA, public BaseB
{
};
int main()
{
Derived d;
BaseA* pa = &d;
BaseB* pb = &d;
BaseB* pbe = (BaseB*)pa; // (BaseB*) 只能在对象头部,进不了虚函数表内部vptr2
BaseB* pbc = dynamic_cast<BaseB*>(pa);
cout << "sizeof(d) = " << sizeof(d) << endl; //8,两个虚函数表指针
cout << "Using pa to call funcA()..." << endl;
pa->funcA(); //funcA
cout << "Using pb to call funcB()..." << endl;
pb->funcB(); //funcB
cout << "Using pbc to call funcB()..." << endl;
pbc->funcB(); //funcA
cout << endl;
cout << "pa = " << pa << endl;
cout << "pb = " << pb << endl;
cout << "pbe = " << pbe << endl;
cout << "pbc = " << pbc << endl;
return 0;
}
Derived d;
BaseA* pa = &d;
BaseB* pb = &d;
BaseB* pbe = (BaseB*)pa; // 虚函数表结构一样,pbb->vptr1 找不到vptr2虚函数表的指针
pa-->vptr1; <--pbb
vptr2; <--pb
解决方案:需要进行强制类型转换时,c++中推荐使用新型类型转换关键字 dynamic_cast.
工程中的“多重继承”方式:单继承某个类+实现(多个)接口
#include <iostream>
#include <string>
using namespace std;
class Base
{
protected:
int mi;
public:
Base(int i)
{
mi = i;
}
int getI()
{
return mi;
}
bool equal(Base* obj) //判断参数指针指向是不是当前对象
{
return (this == obj);
}
};
class Interface1 //接口
{
public:
virtual void add(int i) = 0; //抽象类->纯虚函数
virtual void minus(int i) = 0;
};
class Interface2
{
public:
virtual void multiply(int i) = 0;
virtual void divide(int i) = 0;
};
class Derived : public Base, public Interface1, public Interface2
{
public:
Derived(int i) : Base(i)
{
}
void add(int i)
{
mi += i;
}
void minus(int i)
{
mi -= i;
}
void multiply(int i)
{
mi *= i;
}
void divide(int i)
{
if( i != 0 )
{
mi /= i;
}
}
};
int main()
{
Derived d(100);
Derived* p = &d;
Interface1* pInt1 = &d; //接口指针
Interface2* pInt2 = &d;
cout << "p->getI() = " << p->getI() << endl; // 100
pInt1->add(10);
pInt2->divide(11);
pInt1->minus(5);
pInt2->multiply(8);
cout << "p->getI() = " << p->getI() << endl; // 40
cout << endl;
cout << "pInt1 == p : " << p->equal(dynamic_cast<Base*>(pInt1)) << endl; //打印出两个1,说明
cout << "pInt2 == p : " << p->equal(dynamic_cast<Base*>(pInt2)) << endl; //pInt1和pInt2指向同一个对象
return 0;
}
一些建议:先继承自一个父类,然后实现多个接口(仅仅定义了公有成员函数原型而已)。父类中提供equal()成员函数,equal()成员函数用于判断参数指针是否指向当前对象。与多重继承相关的强制类型转换用 dynamic_case 完成。
多继承中可能出现多个虚函数表指针,与多重继承相关的强制类型转换用 dynamic_cast 完成,工程中用单继承多接口的方式使用多继承。父类提供成员函数用于判断指针是否指向当前对象。