51、c++对象模型分析(下)

在c++编译器的内部类可以理解为结构体,子类是由父类成员叠加子类新成员得到的。

#include <iostream>
#include <string>
using namespace std;
class Demo
{
protected:
    int mi;
    int mj;
public:
    virtual void print()
    {
        cout << "mi = " << mi << ", "
             << "mj = " << mj << endl;
    }
};
class Derived : public Demo
{
    int mk;
public:
    Derived(int i, int j, int k)
    {
        mi = i;
        mj = j;
        mk = k;
    }    
    void print()
    {
        cout << "mi = " << mi << ", "
             << "mj = " << mj << ", "
             << "mk = " << mk << endl;
    }
};
struct Test
{
    void* p;       //  指针存在于最初的4个字节上
    int mi;
    int mj;
    int mk;
};
int main()
{
    cout << "sizeof(Demo) = " << sizeof(Demo) << endl;           //8+4(虚函数表)
    cout << "sizeof(Derived) = " << sizeof(Derived) << endl;     //12+4(虚函数表指针,指针存在最初的4个字节上)
    Derived d(1, 2, 3);
    Test* p = reinterpret_cast<Test*>(&d);      //说明内存分布是一致的 
    cout << "Before changing ..." << endl;    
    d.print();
    p->mi = 10;
    p->mj = 20;
    p->mk = 30;    
    cout << "After changing ..." << endl;    
    d.print();    
    return 0;
}

 多态是面向对象理论中的概念,他和具体的程序设计语言是没有关系的,也就是说多态指的仅仅是相同的行为方式不同的行为结果,表现出多种形态,在c++中就是由虚函数实现的。相同的行为方式就是指同一条语句不同行为结果就是指得到的结果是不同的。c++是面向对象语言,所以有多态的表现形式,通过虚函数实现。

c++多态的实现原理:当类中声明虚函数时,编译器会在类中生成一个虚函数表,虚函数表是一个存储成员函数地址的数据结构。虚函数表是由编译器自动生成与维护的。virtural成员函数会被编译器放入虚函数表中。存在虚函数时,每个对象中都有一个指向虚函数表的指针。

class demo

{

    int mi,mj;

public:

    virtual int add(int value)            ==>虚函数表:此函数地址void demo::add(int value)

     {

            return mi+mj+value;

     }

};

class derived:public demo

{

    int mk;

public:

        virtual int add(int value)      ==>也生成一张虚函数表:放入此函数地址void derived::add(int value)

{

      return mk+value;         

}

};

创建demo对象时,编译器向此对象放入vptr指针成员变量,指向虚函数表void demo::add(int value)

创建derived对象时,编译器也放入一个vptr指针,指向虚函数表void derived::add(int value)

void run(demo *p,int v)

{

p->add(v); =>编译器确认add()是否为虚函数,是:编译器在当前对象中的vptr所指向的虚函数表中查找add()的地址

                           ==>否:编译器可以直接确定被调用成员的函数地址、

}

void run(demo* p, int v)

{

    p->add(v);         // p->具体对象(vptr)->虚函数表->函数地址

}

调用效率对比:虚函数<普通成员函数

用c实现多态:

#ifndef _51_2_H_
#define _51_2_H_
typedef void Demo;
typedef void Derived;
Demo* Demo_Create(int i, int j);
int Demo_GetI(Demo* pThis);
int Demo_GetJ(Demo* pThis);
int Demo_Add(Demo* pThis, int value);
void Demo_Free(Demo* pThis);
Derived* Derived_Create(int i, int j, int k);
int Derived_GetK(Derived* pThis);
int Derived_Add(Derived* pThis, int value);

#endif

#include "51-2.h"
#include "malloc.h"
static int Demo_Virtual_Add(Demo* pThis, int value);
static int Derived_Virtual_Add(Demo* pThis, int value);
struct VTable     // 2. 定义虚函数表数据结构
{
    int (*pAdd)(void*, int);   // 3. 虚函数表里面存储什么?  (*pAdd)函数指针
};
struct ClassDemo
{
    struct VTable* vptr;     // 1. 定义虚函数表指针  ==》 虚函数表指针类型???
    int mi;
    int mj;
};
struct ClassDerived
{
    struct ClassDemo d;
    int mk;
};
static struct VTable g_Demo_vtbl = 
{
    Demo_Virtual_Add
};
static struct VTable g_Derived_vtbl = 
{
    Derived_Virtual_Add
};
Demo* Demo_Create(int i, int j)
{
    struct ClassDemo* ret = (struct ClassDemo*)malloc(sizeof(struct ClassDemo)); 
    if( ret != NULL )
    {
        ret->vptr = &g_Demo_vtbl;   // 4. 关联对象和虚函数表
        ret->mi = i;
        ret->mj = j;
    }    
    return ret;
}
int Demo_GetI(Demo* pThis)
{
     struct ClassDemo* obj = (struct ClassDemo*)pThis;    
     return obj->mi;
}
int Demo_GetJ(Demo* pThis)
{
    struct ClassDemo* obj = (struct ClassDemo*)pThis;
    return obj->mj;
}
// 6. 定义虚函数表中指针所指向的具体函数
static int Demo_Virtual_Add(Demo* pThis, int value)
{
    struct ClassDemo* obj = (struct ClassDemo*)pThis;    
    return obj->mi + obj->mj + value;
}
// 5. 分析具体的虚函数!!!!
int Demo_Add(Demo* pThis, int value)
{
    struct ClassDemo* obj = (struct ClassDemo*)pThis;
    return obj->vptr->pAdd(pThis, value);
}
void Demo_Free(Demo* pThis)
{
    free(pThis);
}
Derived* Derived_Create(int i, int j, int k)
{
    struct ClassDerived* ret = (struct ClassDerived*)malloc(sizeof(struct ClassDerived));    
    if( ret != NULL )
    {
        ret->d.vptr = &g_Derived_vtbl;
        ret->d.mi = i;
        ret->d.mj = j;
        ret->mk = k;
    }
    
    return ret;
}
int Derived_GetK(Derived* pThis)
{
    struct ClassDerived* obj = (struct ClassDerived*)pThis;
    
    return obj->mk;
}
static int Derived_Virtual_Add(Demo* pThis, int value)
{
    struct ClassDerived* obj = (struct ClassDerived*)pThis; 
    return obj->mk + value;
}
int Derived_Add(Derived* pThis, int value)
{   
    struct ClassDerived* obj = (struct ClassDerived*)pThis;
    
    return obj->d.vptr->pAdd(pThis, value);

}


#include "stdio.h"
#include "51-2.h"
void run(Demo* p, int v)
{
    int r = Demo_Add(p, v);
    
    printf("r = %d\n", r);
}
int main()
{
    Demo* pb = Demo_Create(1, 2);
    Derived* pd = Derived_Create(1, 22, 333);    
    printf("pb->add(3) = %d\n", Demo_Add(pb, 3));
    printf("pd->add(3) = %d\n", Derived_Add(pd, 3));   
    run(pb, 3);
    run(pd, 3);   
    Demo_Free(pb);
    Demo_Free(pd);    
    return 0;

}

继承的本质就是父子间成员变量的叠加。

c++中的多态是通过虚函数表实现的。虚函数表是由编译器自动生成与维护的,虚函数的调用效率低于普通成员函数。

猜你喜欢

转载自blog.csdn.net/ws857707645/article/details/80257986