zoukankan      html  css  js  c++  java
  • C++对象内存布局⑨VS编译器虚拟继承菱形继承

    C++对象内存布局--⑨VS编译器--虚拟继承--菱形继承

    //VS编译器--虚拟继承--菱形继承.cpp
    //2010.8.19
    //VS编译器
    #include <iostream>
    using namespace std;
    ////////////////////////////////////////////////////////////////
    class Base
    {
        public:
            Base(int a = 10):a(a)
            {
            }
            virtual void show()
            {
                cout << "Base::show()" << "\t" << a << endl;
            }
            virtual void testA()
            {
                cout << "Base::testA()" <<"\t" << a << endl;
            }
        private:
            int a;
    };
    ////////////////////////////////////////////////////////////////
    class BaseA : virtual public Base
    {
        public:
            BaseA(int b = 20):b(b)
            {
            }
            virtual void showA()
            {
                cout << "BaseA::showA()" << "\t" << b << endl;
            }
            void testA()
            {
                cout << "BaseA::testA()" << "\t" << b << endl;
            }
        private:
            int b;
    };
    ////////////////////////////////////////////////////////////////
    class BaseB : virtual public Base
    {
        public:
            BaseB(int c = 30):c(c)
            {
            }
            virtual void showB()
            {
                cout << "BaseB::showB()" << "\t" << c << endl;
            }
        private:
            int c;
    };
    ////////////////////////////////////////////////////////////////
    class Derived : public BaseA, public BaseB
    {
        public:
            Derived(int d = 40):d(d)
            {
            }
            virtual void show()
            {
                cout << "Derived::show()" << "\t" << d << endl;
            }
            virtual void test()
            {
                cout << "Derived::test()" << "\t" << d << endl;
            }
        private:
            int d;
    };
    ////////////////////////////////////////////////////////////////
    int main()
    {
        Derived obj;
        int** p = (int**)&obj;
        cout << "Derived obj sizeof = " << sizeof(obj) << endl;
        cout << "Derived obj 内存布局:" << endl;
        for (int i = 0; i != sizeof(obj)/4; ++i)
        {
            cout << p[i] << endl;
        }
        cout << endl;
       typedef void (__thiscall *fun)(void*pThis);//非常重要
    	/*通过虚函数表调用函数
    	第一个虚函数表指针。BaseA*/
    	((fun)(p[0][0]))(p);
    	((fun)(p[0][1]))(p);
    
    	//第二个虚函数表指针。BaseB
    	cout << endl;
    	((fun)(p[3][0]))(p+3);
    	//第三个虚函数表指针。Base
        cout << endl;
    	((fun)(p[8][0]))(p+8);
    	((fun)(p[8][1]))(p+8);
    	system("pause");
        return 0;
    }
    /*
    Derived obj sizeof = 40
    Derived obj 内存布局:
    0041A200
    0041A210
    00000014
    0041A1F8
    0041A208
    0000001E
    00000028
    00000000
    0041A1EC
    0000000A
    
    BaseA::showA()  20
    Derived::test() 40
    
    BaseB::showB()  30
    
    Derived::show() 40
    BaseA::testA()  20
    */
    
  • 相关阅读:
    上传预览图片
    css3图片3D翻转效果
    获取新浪天气api显示天气情况(转)
    matlab添加M_map工具箱(转 http://blog.sina.com.cn/s/blog_491b86bf0100srt9.html)
    Matlab read_grib.r4 安装新方法(转自:http://blog.sina.com.cn/s/blog_9f36648b010179s7.html)
    grb文件的读取
    Windows 7 防火墙对Virtualbox guest的影响
    Virtualbox修改bios信息安装Windows XP OEM
    Wordpress编辑器(Tinymce)在Chrome中动态修改图片大小
    Android流行的框架整理
  • 原文地址:https://www.cnblogs.com/cswuyg/p/1804105.html
Copyright © 2011-2022 走看看