• C++-多重继承的注意点


    1, 钻石型多重继承如果不想要底部的类有重复的变量,则需要声明为virtual继承

      class File{...};

      class InputFile: virtual public File{..};

      class OutputFile: virtual public File{....};

      class IOFile: public InputFile,

            public OutputFile

      {...};

    2, 多重继承来的成员函数指向一边的指针不能访问另一边的函数

    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   multi_inherit.h
    //  Version     :   0.10    created    2013/11/08 00:00:00        
    //  Author      :   Jimmy Han
    //  Comment     :  
    //
    ///////////////////////////////////////////////////////////////////////////////
    #ifndef MULTI_INHERIT_H
    #define    MULTI_INHERIT_H
    
    class base1{
    public:
        void baseFunc();
    private:
        int baseElem;
    };
    
    class derive1 : public base1 {
    public:
        void derive1Func();
    private:
        int derive1Elem;
    };
    
    class base2{
    public:
        void baseFunc();
    private:
        int baseElem;
    };
    
    class derive2 : public derive1, public base2{
    public:
        void derive2Func();
        
        //to avoid ambigous
        void baseFunc();
        
    private:
        int derive2Elem;
    };
    
    #endif
    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   multi_inherit.cpp
    //  Version     :   0.10
    //  Author      :   Ryan Han
    //  Date        :   2013/07/26 16:50:14
    //  Comment     :  
    //
    ///////////////////////////////////////////////////////////////////////////////
    #include "multi_inherit.h"
    
    #include <iostream>
    using namespace std;
    
    void base1::baseFunc() {
        cout << "base1Func was called. " << endl;
    }
    
    void base2::baseFunc() {
        cout << "base2Func was called. " << endl;
    }
    
    void derive1::derive1Func() {
        cout << "derive1Func was called. " << endl;
    }
    
    void derive2::derive2Func() {
        cout << "derive2Func was called. " << endl;
    }
    
    void derive2::baseFunc() {
        base2::baseFunc();
    }
    ///////////////////////////////////////////////////////////////////////////////
    //
    //  FileName    :   multi_inherit_client.cpp
    //  Version     :   0.10
    //  Author      :   Ryan Han
    //  Date        :   2013/07/26 16:50:14
    //  Comment     :  
    //    Output        :
    //    $ ./a
    // $ ./a.exe
    // base2Func was called.
    // derive1Func was called.
    // derive2Func was called.
    // base1Func was called.
    // derive1Func was called.
    
    ///////////////////////////////////////////////////////////////////////////////
    #include "multi_inherit.h"
    #include <iostream>
    using namespace std;
    
    int main() {
        derive2 d2;
        //if not embed, this will be ambigous and compile error
        d2.baseFunc();
        //could call both derive1Func and derive2Func
        d2.derive1Func();
        d2.derive2Func();
        
        derive1* b1 = new derive2;
        //call base1 or derive1 functions, OK
        b1->baseFunc();
        b1->derive1Func();
        //call base2 or derive2 functions, compile error.
        //b1->derive2Func();
        
        return 0;
    }
    
    
    
        
  • 相关阅读:
    170815、redis3.0安装配置
    170814、Java使用gzip压缩文件、还原文件
    170811、Java获取jdk系统环境变量
    170810、spring+springmvc+Interceptor+jwt+redis实现sso单点登录
    加密概述
    软件测试入门第02天:基础理论知识
    软件测试入门第01天:综述
    【心路历程】永远热泪盈眶
    Linux部署Django:报错 nohup: ignoring input and appending output to ‘nohup.out’
    【WEB】jQuery 判断复选框是否选中
  • 原文地址:https://www.cnblogs.com/dracohan/p/3813655.html
Copyright © 2020-2023  润新知