C++笔记 第五十四课 被遗弃的多重继承(下)---狄泰学院

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42187898/article/details/84640126

如果在阅读过程中发现有错误,望评论指正,希望大家一起学习,一起进步。
学习C++编译环境:Linux

第五十四课 被遗弃的多重继承(下)

1.多重继承的问题三

多重继承可能产生多个虚函数表
在这里插入图片描述

54-1 多重继承问题三

#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);  //use this
    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 pbb to call funcB()..." << endl;
    pbc->funcB();
    cout << endl;
    cout << "pa = " << pa << endl;
    cout << "pb = " << pb << endl;
    cout << "pbe = " << pbe << endl;
    cout << "pbc = " << pbc << endl;
    return 0;
}
运行结果
sizeof(d) = 16
Using pa to call funcA()...
BaseA::funcA()
Using pb to call funcB()...
BaseB:: funcB()
Using pbb to call funcB()...
BaseB:: funcB()
pa = 0x7fff843450d0
pb = 0x7fff843450d8
pbe = 0x7fff843450d0
pbc = 0x7fff843450d8

需要进行强制类型转换时,C++中推荐使用新式类型转换关键字!!
解决方案:dynamic_cast
在这里插入图片描述
程序运行本质:
通过pa调用funcA的过程:从pa里面拿到pa的地址,通过地址找到虚函数表指针vptr1,进而通过虚函数表指针找对象的函数地址,进而找到funA的地址。
通过pb调用funcB的过程:从pb里面拿到pb的地址,通过地址找到虚函数表指针vptr2,进而通过虚函数表指针所指向的虚函数表里面得到虚函数地址,进而找到funB的地址。
vptr1,vptr2两个虚函数表指针所指向的虚函数表在结构上是一样的,因此通过pbb调用funcB的过程:首先得到地址,得到的地址在pbb所指向的位置,然后找虚函数表指针,找到的虚函数表指针vptr1,就在pbb所指向的虚函数表里面找funcB()的地址,在这里面是找不到funcB()的地址,能够找到的是funcA()的地址,并且这个地址是完全正确没有误差的。

2.正确的使用多重继承

工程开发中的“多重继承”方式:
单继承某个类 + 实现(多个)接口
在这里插入图片描述

54-2 正确的多继承方式—单继承、多接口进行面向对象的设计

#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);    //110
    pInt2->divide(11); //10
    pInt1->minus(5);   //5
    pInt2->multiply(8);//40
    cout << "p->getI() = " << p->getI() << endl;  //40
    cout << endl;
    cout << "pInt1 == p : " << p->equal(dynamic_cast<Base*>(pInt1)) << endl;
    cout << "pInt2 == p : " << p->equal(dynamic_cast<Base*>(pInt2)) << endl;
    return 0;
}
运行结果
p->getI() = 100
p->getI() = 40
pInt1 == p : 1
pInt2 == p : 1

一些有用的工程建议—非常有用的技巧
先继承字一个父类,然后实现多个接口
父类中提供equal()成员函数
equal()成员函数用于判断指针是否指向当前对象
与多重继承相关的强制类型转换用dynamic_cast完成
小结
多继承中可能出现多个虚函数表指针
与多重继承相关的强制类型转换用dynamic_cast完成
工程开发中采用“单继承多接口”的方式使用多继承
父类提供成员函数用于判断指针是否指向当前对象

猜你喜欢

转载自blog.csdn.net/weixin_42187898/article/details/84640126