c语言实现c++继承模型

要用c语言实现c++继承模型,首先需要回顾一些关于类,继承相关的理论知识:

1. class的本质仍是struct, 在内存中class依旧可以看作变量的集合,每个对象有独立的成员变量,所有对象共享类中的成员函数, 类中的成员函数位于代码段中。

2. 运行时的对象退化为结构体的形式 ,访问权限关键字在运行时失效, 访问权限控制是编译期的限制。

3. 调用成员函数时对象地址作为参数隐式传递 (函数编程=》面向对象编程 的原理),成员函数通过对象地址访问成员变量。

4. class的继承通过对象中的成员-虚函数表实现,通过将每个对象中虚函数表中的函数指针指向不同函数完成第一层多态, 通过void*万能指针转换成具体对象指针完成第二层多态。


给出一个具体的class继承样例:

#include 
class Demo
{
public:
    Demo(int i, int j){m_i = i; m_j = j;}
    ~Demo(){}
    int GetI(){ return m_i; }
    int GetJ(){ return m_j; }
    virtual int Add(int value) { return m_i + m_j + value; }
    int m_i;
    int m_j;
};

class Derived : public Demo
{
public:
    Derived(int i, int j, int k):Demo(i,j){ m_k = k; }
    ~Derived(){}
    int GetK() { return m_k; }
    virtual int Add(int value) { return m_i + m_j + m_k + value; }
    int m_k;
};

int main()
{
    int value = 5;

    Demo* pDemo = new Demo(1, 2);
    printf("Demo i = %d, j = %d\n", pDemo->GetI(), pDemo->GetJ());
    printf("Demo Add %d => %d\n", value, pDemo->Add(value));
    delete pDemo;

    Derived* pDerived = new Derived(1, 2, 3);
    printf("Derived i = %d, j = %d k = %d\n", pDerived->GetI(), pDerived->GetJ(), pDerived->GetK());
    printf("Derived Add %d => %d\n", value, pDerived->Add(value));
    delete pDerived;

    return 0;
}

接下来用c实现上面的代码:

#include 
#include 

typedef void Demo;
typedef void Derived;
typedef int(*VTable)(void*, int);

///////////////////////////////////////////////

struct ClassDemo{
    VTable m_vtable;
    int m_i;
    int m_j;
};

static int Demo_virtual_Add(Demo* pThis, int value)
{
    ClassDemo* cDemo = (struct ClassDemo*)pThis;
    return cDemo->m_i + cDemo->m_j + value;
}

Demo* Demo_Create(int i, int j)
{
    ClassDemo* cDemo = (struct ClassDemo*)malloc(sizeof(ClassDemo));
    if(cDemo == 0)
    {
        return NULL;
    }
    cDemo->m_i = i;
    cDemo->m_j = j;
    cDemo->m_vtable = &Demo_virtual_Add;
    return cDemo;
}

void Demo_Free(Demo* pThis)
{
    free(pThis);
}

int Demo_GetI(Demo* pThis)
{
    ClassDemo* cDemo = (struct ClassDemo*)pThis;
    return cDemo->m_i;
}

int Demo_GetJ(Demo* pThis)
{
    ClassDemo* cDemo = (struct ClassDemo*)pThis;
    return cDemo->m_j;
}

int Demo_Add(Demo* pThis, int value)
{
    ClassDemo* cDemo = (struct ClassDemo*)pThis;
    return cDemo->m_vtable(pThis, value);
}

/////////////////////////////////////////////////

struct ClassDerived{
    struct ClassDemo m_demo;
    int m_k;
};

static int Derived_virtual_Add(Derived* pThis, int value)
{
    ClassDerived* cDerived = (struct ClassDerived*)pThis;
    return cDerived->m_demo.m_i + cDerived->m_demo.m_j + cDerived->m_k + value;
}

Derived* Derived_Create(int i, int j, int k)
{
    ClassDerived* cDerived = (struct ClassDerived*)malloc(sizeof(ClassDerived));
    if(cDerived == 0)
    {
        return NULL;
    }
    cDerived->m_k = k;
    cDerived->m_demo.m_i = i;
    cDerived->m_demo.m_j = j;
    cDerived->m_demo.m_vtable = &Derived_virtual_Add;
    return cDerived;
}

int Derived_GetK(Derived* pThis)
{
    ClassDerived* cDerived = (struct ClassDerived*)pThis;
    return cDerived->m_k;
}

int Derived_Add(Derived* pThis, int value)
{
    ClassDerived* cDerived = (struct ClassDerived*)pThis;
    return cDerived->m_demo.m_vtable(pThis, value);
}

/////////////////////////////////////////////////////////////

int main()
{
    int value = 5;

    Demo* pDemo = Demo_Create(1, 2);
    printf("Demo i = %d, j = %d\n", Demo_GetI(pDemo), Demo_GetJ(pDemo));
    printf("Demo Add %d => %d\n", value, Demo_Add(pDemo, value));
    Demo_Free(pDemo);

    Derived* pDerived = Derived_Create(1, 2, 3);
    printf("Derived i = %d, j = %d, k = %d\n", Demo_GetI(pDerived), Demo_GetJ(pDerived), Derived_GetK(pDerived));
    printf("Derived Add %d => %d\n", value, Derived_Add(pDerived, value));
    Demo_Free(pDerived);

    return 0;
}


你可能感兴趣的:(c/c++)