zoukankan      html  css  js  c++  java
  • 第51课 继承对象模型分析——多态的本质分析

    多态的本质分析

    用C写面向对象,用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. 虚函数表里面存储什么???
    };
    
    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
    ", r);
    }
    
    int main()
    {
        Demo* pb = Demo_Create(1, 2);
        Derived* pd = Derived_Create(1, 22, 333);
        
        printf("pb->add(3) = %d
    ", Demo_Add(pb, 3));
        printf("pd->add(3) = %d
    ", Derived_Add(pd, 3));
        
        run(pb, 3);
        run(pd, 3);
        
        Demo_Free(pb);
        Demo_Free(pd);
        
        return 0;
    }

    继承的本质就是父子间成员变量的叠加
    C++中的多态是通过虚函数表实现的
    虚函数表是由编译器自动生成与维护的
    虚函数的调用效率低于普通成员函数

  • 相关阅读:
    Linux C/C++编程之(三)常用命令之文件处理命令
    linux下查看activiti流程图乱码
    基于区块链的自主身份和可信声明
    数字证书PKI原理
    Linux C/C++编程之(二)常用命令之目录处理命令
    Linux C/C++编程之(一)VMware 虚拟机安装Ubuntu16.04 图解
    移动端专项测试-内存泄漏
    centos7 netstat命令使用场景 杂记
    《java入门第一季》之面向对象(构造方法)
    《java入门第一季》之面向对象this关键字
  • 原文地址:https://www.cnblogs.com/-glb/p/11967244.html
Copyright © 2011-2022 走看看