zoukankan      html  css  js  c++  java
  • 第53课 被遗弃的多重继承 (下)——正确的使用多重继承

    工程开发中的多重继承方式:(这是面向对象理论中所推荐的方式)
    单继承某个类 + 实现(多个)接口

    #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 //从表象上来看,这依旧是C++中的多重继承,但是从语义上来看,这就不是多继承了。
                                                                      //是单继承+实现多个接口
    {
    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; //一定要熟记,与继承相关、与虚函数相关的强制类型转换,一定要使用dynamic_cast
        cout << "pInt2 == p : " << p->equal(dynamic_cast<Base*>(pInt2)) << endl;
    
        return 0;
    }

    在父类中添加一个成员函数,用于解决在多继承的情况下指向同一个对象的多个指针它们的值不一样。
    定义接口class Interface1、class Interface2这样的方式是工程中常用的一种解决方案。
    但是class Derived : public Base, public Interface1, public Interface2 表象还是多重继承,同一个子类继承了多个父类。
    在这里我们使用了多个接口,必然在Interface1和Interface2中有虚函数。既然有虚函数,又与继承相关,就可以借助dynamic_cast这个关键字。有了这个关键字,编译器就会为我们重新计算指针的值,这样就可以在一定程度上避免:在多继承发生的情况下,指向同一个对象的指针值可能不同。
    工程是如何来做的呢?
    在顶层的父类那个地方,定义一个成员函数equal,用来判断参数指针是不是指向当前的对象。

    使用方式,见main函数

    如果在main函数中,不使用dynamic_cast关键字:

    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(pInt1) << endl;
        cout << "pInt2 == p : " << p->equal(pInt2) << endl;
    
        return 0;
    }

    出现下面的错误:

    一些有用的工程建议
    -先继承一个父类,然后实现一个接口
    -父类中提供equal()成员函数
    -equal成员函数用于判断指针是否指向当前对象
    -与多重继承相关的强制类型转换用dynamic_cast完成

    小结:
    多继承中可能出现多个虚函数表指针
    与多重继承相关的强制类型转换用dynamic_cast完成
    工程开发中采用单继承多接口的方式使用多继承
    父类提供成员函数用于判断指针是否指向当前对象

  • 相关阅读:
    Redis-高级教程-Java 使用
    Redis-高级教程-分区
    Redis-高级教程-管道技术
    Redis-高级教程-客户端连接
    Redis-高级教程-性能测试
    Redis-高级教程-安全
    Redis-高级教程-数据备份与恢复
    Redis-命令-Stream
    NG-ZORRO + Angular11增加自定义全局样式,不影响其他页面全局样式,仅作用于当前页面
    VUE上传表格文件发送后端,后端解析以及上传文件,前端进行解析的实现方法
  • 原文地址:https://www.cnblogs.com/-glb/p/11968397.html
Copyright © 2011-2022 走看看