zoukankan      html  css  js  c++  java
  • C++虚继承 之 内存布局

    1. 概述

    C++虚继承的作用:避免多重继承时因基类的重复而引起的调用二义性.

    2. 多重继承 + 非虚继承

    #define MemoryLayout_H
    #ifdef MemoryLayout_H
    
    class Base
    {
    public:
        void CommonFunction();
        virtual void VirtualFunction1();
        virtual void VirtualFunction2();
    private:
        int m_baseVar1;
        int m_baseVar2;
    };
    
    //class Derived1:virtual public Base
    class Derived1 :public Base
    {
    public :
        void Derived1CommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void Derived1VirtualFunction();
    private:
        int m_derived1Var;
    };
    
    //class Derived2 :virtual public Base
    class Derived2 :public Base
    {
    public:
        void Derived2CommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void Derived2VirtualFunction();
    private:
        int m_derived2Var;
    };
    
    class DerivedDerived :public Derived1,public Derived2
    {
    public:
        void DerivedCommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void DerivedDerivedVirtualFunction();
    private:
        int m_derivedDerivedVar;
    };
    
    #endif/*MemoryLayout_H*/

    利用vs2015输出其内存布局分别为:

    Base:

     Derived1:

    Derived2:

     

    DerivedDerived:

    内存布局分析:
    这里主要分析DerivedDerived类的内存布局:

    此继承关系中,若通过DD类实例调用VF1函数,会产生歧义.

    3. 多重继承+虚继承

    #define MemoryLayout_H
    #ifdef MemoryLayout_H
    
    class Base
    {
    public:
        void CommonFunction();
        virtual void VirtualFunction1();
        virtual void VirtualFunction2();
    private:
        int m_baseVar1;
        int m_baseVar2;
    };
    
    class Derived1:virtual public Base
    //class Derived1 :public Base
    {
    public :
        void Derived1CommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void Derived1VirtualFunction();
    private:
        int m_derived1Var;
    };
    
    class Derived2 :virtual public Base
    //class Derived2 :public Base
    {
    public:
        void Derived2CommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void Derived2VirtualFunction();
    private:
        int m_derived2Var;
    };
    
    class DerivedDerived :public Derived1,public Derived2
    {
    public:
        void DerivedCommonFunction();
        //virtual void VirtualFunction1();
        virtual void VirtualFunction2();
        virtual void DerivedDerivedVirtualFunction();
    private:
        int m_derivedDerivedVar;
    };
    
    #endif/*MemoryLayout_H*/

    利用vs2015输出其内存布局分别为:

    Base:

     Base类的内存布局与非虚继承时相同.

    Derived1:

     Derived1内存布局分析:

     

    Derived2:

    Derived2内存布局与Derived1相同.

    DerivedDerived:

    DerivedDerived内存布局分析:

    这里是否缺少一个信息,若通过指针调用Derived2的DerivedDerivedVirtualFunction函数,应该怎么处理?没有展示出来. 




  • 相关阅读:
    "饼状图/环形图/玫瑰图"组件:<chart-pie> —— 快应用组件库H-UI
    "柱状图"组件:<chart-bar> —— 快应用组件库H-UI
    "折线图"组件:<chart-line> —— 快应用组件库H-UI
    "进度条"组件:<chart-progress> —— 快应用组件库H-UI
    "警告提示"组件:<alert> —— 快应用组件库H-UI
    "全局提示"组件:<message> —— 快应用组件库H-UI
    "滑动操作"组件:<swipe-action> —— 快应用组件库H-UI
    Git使用总结之修改了用户名之后git无法使用
    Facebook授权登录
    Android键盘显示和隐藏
  • 原文地址:https://www.cnblogs.com/jingjingblog/p/9228274.html
Copyright © 2011-2022 走看看