本文主要是介绍类的继承——被遗弃的多重继承,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
本文参照于狄泰软件学院,唐佐林老师的——《C++深度剖析教程》
-
-
- 多重继承之对象地址问题
- 多重继承之冗余成员问题
- 多重继承之虚函数表问题
-
关联知识:virtual虚函数,继承
问题:C++中是否允许一个类继承自多个父类?如果可以,那么会出现什么问题呢?如果不可以,为什么?
我们在单继承的语法基础上,再继承两个父类,试试编译是否能通过。
class Derived : public BaseA,public BaseB,public BaseC
{//TODO:
};
发现可以通过编译,那么我们可以确定C++中支持多重继承的代码。也就是多重继承也有单重继承的特性。
1. 一个子类可以拥有多个父类
2. 子类拥有所有父类的成员变量
3. 子类继承所有父类的成员函数
4. 子类对象可以当做任意父类对象使用
问题:多重继承会出现什么问题吗?
多重继承之对象地址问题
通过多重继承得到的对象可能拥有“不同的地址”!
示例代码:
#include <iostream>
#include <string>using namespace std;class BaseA
{int ma;
public:BaseA(int a){ma = a;}int getA(){return ma;}
};class BaseB
{int mb;
public:BaseB(int b){mb = b;}int getB(){return mb;}
};class Derived : public BaseA, public BaseB
{int mc;
public:Derived(int a, int b, int c) : BaseA(a), BaseB(b){mc = c;}int getC(){return mc;}void print(){cout << "ma = " << getA() << ", "<< "mb = " << getB() << ", "<< "mc = " << mc << endl;}
};int main()
{cout << "sizeof(Derived) = " << sizeof(Derived) << endl; // 12Derived d(1, 2, 3);d.print();cout << "d.getA() = " << d.getA() << endl;cout << "d.getB() = " << d.getB() << endl;cout << "d.getC() = " << d.getC() << endl;cout << endl;void* paa = pa;void* pbb = pb;if( paa == pbb ){cout << "Pointer to the same object!" << endl;}else{cout << "Error" << endl;}cout << "pa = " << pa << endl;cout << "pb = " << pb << endl;cout << "paa = " << paa << endl;cout << "pbb = " << pbb << endl;return 0;
}
输出结果:
sizeof(Derived) = 12
ma = 1, mb = 2, mc = 3
d.getA() = 1
d.getB() = 2
d.getC() = 3Error
pa = 0xbfd441a4
pb = 0xbfd441a8
paa = 0xbfd441a4
pbb = 0xbfd441a8
从输出结果我们可以知道:
1. pa和pb明明指向同一个对象d,但是地址指向的地址却不同。所以,通过多重继承得到的对象可能拥有“不同的地址”!
没有解决方案!!!
多重继承之冗余成员问题
多重继承可能产生冗余的成员,我们设想一下这种情况。
Docter类继承了Teacher和Stduent这两个类。这两个父类继承了People类中的函数时,那么在调用父类函数时,将会调用哪个父类的函数呢?
当我们调用了这个函数时,编译器会报错。原因就是编译器不知道到底应该调用哪个类的print()函数。产生错误。
而这种数据冗余问题就是当多重继承关系出现闭合时发生!
问题:我们如何解决这个问题呢?
思路:我们曾经在继承中遇到同名覆盖的问题。当时的解决方案是使用virtual关键字使父类的同名函数编程虚函数。那么我们是否可以用相同的方法解决呢?
示例代码:虚继承
#include <iostream>
#include <string>using namespace std;class People
{protected:string m_name;int m_age;
public:People(string name, int age){m_name = name;m_age = age;}void print(){cout << "Name = " << m_name << ", "<< "Age = " << m_age << endl;}
};class Teacher : virtual public People
{
public:Teacher(string name, int age) : People(name, age){}};class Student : virtual public People
{
public:Student(string name, int age) : People(name, age){}
};class Doctor : public Teacher, public Student
{
public:Doctor(string name, int age) : Teacher(name, age), Student(name, age), People(name, age){}
};int main()
{Doctor d("Delphi", 33);d.print(); //Errorreturn 0;
}
输出结果:
Name = Delphi, Age = 33
用虚继承的方法能够解决数据冗余的问题。
1. 使得中间层父类不再关心顶层父类的初始化
2. 最终子类必须直接调用顶层父类的构造函数
但是问题是:当架构设计中需要继承时,无法确定使用直接继承还是虚继承!
多重继承之虚函数表问题
多重继承可能产生多个虚函数表
示例代码:
#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; // oops!!//BaseB* pbc = dynamic_cast<BaseB*>(pa);cout << "sizeof(d) = " << sizeof(d) << endl;cout << "Using pa to call funcA()..." << endl;pa->funcA();cout << "Using pb to call funcB()..." << endl;pb->funcB();cout << "Using pb to call funcB()..." << endl;pbe->funcB();cout << endl;cout << "pa = " << pa << endl;cout << "pb = " << pb << endl;cout << "pbe = " << pbe << endl;return 0;
}
输出结果:
sizeof(d) = 8
Using pa to call funcA()…
BaseA::funcA()
Using pb to call funcB()…
BaseB::funcB()
Using pb to call funcB()…
BaseA::funcA()pa = 0xbff7094c
pb = 0xbff70950
pbe = 0xbff7094c
分析:
1. 由BaseB* pbe = (BaseB*)pa;可知,我们是想把pa对象强制类型转换为BaseB*类型。但是从输出BaseA::funcA()结果来看,pbe依然指向的是pa对象。
2. 从打印pa,pb,pbe地址来看,pbe的确依然指向了pa所指的地址,并没有强制类型转换成功。所以输出的是BaseA类的成员函数。
实际上这种强制类型转换是C语言中的方式,在C++中并不是用于多继承的情况。那么如何解决呢?C++为我们提供了新式类型转换。
用于继承的强制类型转换关键字为:dynamic_cast。
这篇关于类的继承——被遗弃的多重继承的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!