用c语言实现面向对象的编程方式

 

主要利用结构体和函数指针,实现面向对象编程中的封装、继承和多态;

//base.h
typedef struct BaseClass
{
    int m_Data;

 void *pComponentPrivate; //指向子类结构体的指针
 
 void (*GetComponentVersion)();

//声明虚函数 
//指针参数,相当于C++成员函数隐含的this指针
    int (*GetParameter)(void* hComponent);
 
    void (*SetParameter)(void* hComponent, int nData);

    void (*ComponentDeInit)(void* hComponent);

}BaseClass;

//对于父类中声明的每个虚函数
//都定义一个具体的实现宏,以实现
//对不同子类的实现函数的统一调用方式

#define ClassGetComponentVersion(hComponent)      ((BaseClass*)hComponent)->GetComponentVersion()
#define ClassGetParameter(hComponent)   ((BaseClass*)hComponent)->GetParameter(hComponent)    /* Macro End */
#define ClassSetParameter(hComponent, nData)  ((BaseClass*)hComponent)->SetParameter(hComponent,nData)    /* Macro End */

//构造函数
void ClassGetHandle(void **pHandle, const char *cComponentName);

//析构函数
void ClassFreeHandle(void *hComponent);

void BaseGetComponentVersion(void);

//base.cpp
#include "stdafx.h"

#include
#include
#include

#include "derivedclasstable.h"

#include "base.h"

static int GetParameter(void* hComponent)
{
    BaseClass * pComponent = NULL;   
    pComponent = (BaseClass *)hComponent;

 return pComponent->m_Data;
}

static void SetParameter(void* hComponent, int nData)
{
    BaseClass * pComponent = NULL;   
    pComponent = (BaseClass *)hComponent;

 pComponent->m_Data = nData;
}

void BaseComponentInit(void *hComponent)
{
    BaseClass* pHandle = (BaseClass *)hComponent;
    
 pHandle->m_Data = 3;

 pHandle->pComponentPrivate = NULL;

 pHandle->GetParameter = GetParameter;
    pHandle->SetParameter = SetParameter;
}

//类对象的初始化函数(构造函数)
void ClassGetHandle(void **pHandle, const char *cComponentName)
{
 
    int i = 0;

 BaseClass *pComponent = NULL;
  
    while (NULL != ClassComponentList[i].cComponentName)
 {
        if (strcmp(cComponentName, ClassComponentList[i].cComponentName) == 0)
  {
           
            /* Component found */
            break;
        }
        i++;
    }
   
    /* Allocate memory for the component handle which is of the type
     * OMX_COMPONENTTYPE and fill in the size and version field of the
     * structure
     */
    *pHandle = NULL;

     //为每一个子类的对象分配内存
    *pHandle = (BaseClass *)malloc (sizeof(BaseClass));

 pComponent = (BaseClass *)(*pHandle);

    pComponent->GetComponentVersion = BaseGetComponentVersion;
  
    //调用子类的初始化函数
    ClassComponentList[i].pComponentInit(*pHandle);  
}

//类的析构函数
void ClassFreeHandle(void *hComponent)
{
    BaseClass* pHandle = (BaseClass *)hComponent;

 if (pHandle->pComponentPrivate != NULL)
 {      
  /* Call the ComponentDeInit function */
    //调用子类的析构函数
  pHandle->ComponentDeInit(hComponent);
 }
   
    free(hComponent);

 pHandle = NULL;
}

void BaseGetComponentVersion()
{
 printf("Version 1.0\n");
}

//derivedclasstable.h
//描述类的结构体数组
typedef struct ClassCOMPONENTLIST
{
   
    char *  cComponentName;
   
    /* The exported ComponentInit function of the component */
    void  (*pComponentInit)(void * hComponent);
   
} ClassCOMPONENTLIST;

/* external definition for the ComponentTable */
extern ClassCOMPONENTLIST ClassComponentList[];

//derivedclasstable.cpp
#include "stdafx.h"
#include "derivedclasstable.h"

/* Add extrnal ComponentInit function declaration here */
extern void BaseComponentInit(void * hComponent);
extern void Derived1ComponentInit(void * hComponent);
extern void Derived2ComponentInit(void * hComponent);

ClassCOMPONENTLIST ClassComponentList[] =
{
 {"Base", &BaseComponentInit},
    {"Derived1", &Derived1ComponentInit},
    {"Derived2", &Derived2ComponentInit},
    {NULL, NULL}
};

//derived1.h
//子类的声明
typedef struct DERIVED1_COMPONENT_PRIVATETYPE
{
    int m_Data1;
}DERIVED1_COMPONENT_PRIVATETYPE;

//derived1.cpp
#include "stdafx.h"

#include
#include

#include "base.h"
#include "derived1.h"

static int GetParameter(void* hComponent)
{
 DERIVED1_COMPONENT_PRIVATETYPE* pPrivateStruct;
    BaseClass * pComponent;   

    pComponent = (BaseClass *)hComponent;
 pPrivateStruct = (DERIVED1_COMPONENT_PRIVATETYPE*)pComponent->pComponentPrivate;

 return pPrivateStruct->m_Data1;
}

static void SetParameter(void* hComponent, int nData)
{
 DERIVED1_COMPONENT_PRIVATETYPE* pPrivateStruct;
    BaseClass * pComponent;   

    pComponent = (BaseClass *)hComponent;
 pPrivateStruct = (DERIVED1_COMPONENT_PRIVATETYPE*)pComponent->pComponentPrivate;

 pPrivateStruct->m_Data1 = nData;
}

static void ComponentDeInit(void *hComponent)
{
    BaseClass* pHandle = (BaseClass *)hComponent;

    free(pHandle->pComponentPrivate);
 pHandle->pComponentPrivate = NULL; 
}

void Derived1ComponentInit(void *hComponent)
{
    BaseClass* pHandle = (BaseClass *)hComponent;
    DERIVED1_COMPONENT_PRIVATETYPE* pPrivateStruct;
    
    /* if need any private data, allocate here  */
    pHandle->pComponentPrivate = NULL;
    //TODO: Need more clarity _disibledevent= NULL;

 return 0;
}

你可能感兴趣的:(c++,编程,语言,c,null,struct,function)