菱形虚拟虚拟继承模型探索

ezeasasclass Person
{
public:
                  virtual void fun6()
                {
                                cout << "Person::fun6()" << endl;
                }
public:
                 int  _name;
                 int _major;
};
class Student :virtual public Person
{
public:
                 virtual void fun1()
                {
                                cout << "Student::fun1()" << endl;
                }
                 virtual void fun2()
                {
                                cout << "Student::fun2()" << endl;

                }


public:
                 int number;
};
class Teacher :virtual public Person
{
public:
                 virtual void fun3()
                {
                                cout << "Teacher::fun3()" << endl;
                }
                 virtual void fun4()
                {
                                cout << "Teacher::fun4()" << endl;
                }


public:
                 int num;
};
class TeachStudent :public Student,public Teacher
{
public:
                 virtual void fun4()
                {
                                cout << "TeachStudent::fun4()" << endl;
                }
                 virtual void fun5()
                {
                                cout << "TeachStudent::fun5()" << endl;
                }


public:
                 int id;
};
Student  类和Teacher类都继承了 Person类, 而TeachStudent又继承了这两个类
                 TeachStudent s1;//定义一个TeachStudent 类S1
           打开内存窗口查看s1的地址如下

菱形虚拟虚拟继承模型探索_第1张图片

通过给具体的对象成员赋值可已查看内存中成员的分布    


                s1._name = 1;//

                s1._major = 2;

                s1.number = 20;

                s1.num = 9;

                s1.id = 10;

a's菱形虚拟虚拟继承模型探索_第2张图片则类在内存中的分布情况如下图:

菱形虚拟虚拟继承模型探索_第3张图片

    则可以画出内存分布图如下:

菱形虚拟虚拟继承模型探索_第4张图片


对其中的指针进行分析。写出代码如下


typedef void (*FUNC) ();

void PrintVtable(int *vTable)

{

                cout << "虚表地址" << vTable << endl;

                 for (int i = 0; vTable[i] != 0; ++i)

                {

                                printf( "第%d个虚函数地址:0x%x,->" , i, vTable[i]);

                                 FUNC f = (FUNC )vTable[i];

                                f();

                }

                

}


void test()

{

                 TeachStudent s1;

//             Student s2;

                 ///s2._name = 6;

                 //s2.number = 12;

                s1._name = 1;

                s1._major = 2;

                s1.number = 20;

                s1.num = 9;

                s1.id = 10;

                 int  *vTable1= (int *)(*(int *)&s1);//

                PrintVtable(vTable1);

                 int *vTable2 = (int *)(*((int *)&s1 + 3));

                 int *vTable3 = (int *)(*((int *)&s1 + 7));


                PrintVtable(vTable2);

                PrintVtable(vTable3);                

}

对其中的虚表指针进行强制类型转换,输出每个虚表指针中的虚函数如下图:菱形虚拟虚拟继承模型探索_第5张图片

还有两个指针:在内存中可以看到其值为:


值为   -4   24;

spacer.gif菱形虚拟虚拟继承模型探索_第6张图片


值为-4   12;

分析可得  其中存的为 当前对象到虚表指针的偏其移地址 以及到共有的虚函数的的偏移地址。

所以可得出虚拟继承在内存中存在方式为:

你可能感兴趣的:(C++,虚拟,深度解剖)