
/**//*Super.h*/
/**//*クラスの定義*/
#ifndef __SUPER_H
#define __SUPER_H

typedef struct CSuper_t...{
/**//*サブークラスのインスタンス*/
void* body; 
/**//*自身のクラス変数*/
int Value;
}CSuper;
extern int Super_GetValue(CSuper*);
#endif目的就是调用Super_GetValue函数。。
body是用来放子类实体的指针的。

/**//*Super.c*/
#include "Super.h"
/**//*クラス関数*/
int Super_GetValue(CSuper* this)
...{
return this->Value;
}
extern void Sub_Initial(CSub*);其实是空的一个函数,还有就是宏定义

/**//*Sub.h*/
#include "Super.h"
/**//*クラスの定義*/
typedef struct CSub_t...{
CSuper super;
}CSub;
/**//*初期化変数*/
extern void Sub_Initial(CSub*);
/**//*マクロの定義*/
#define Sub_GetValue(p) Super_GetValue(p);
初始化子类的同时,给super的body

/**//*Sub.c*/
#include "Sub.h"
#include "Super.h"
/**//*初期化関数*/
void Sub_Initial(CSub* this)
...{
this->super.body = this;
}
#include "sub.h"
#include "super.h"
CSub mSub;
CSub *pSub;
CSuper *pSuper;

void main()...{
int ret=0;
/**//*ポインタの初期化*/
pSub = &mSub;
Sub_Initial(pSub);
/**//*継承されたクラスの変数を利用する*/
ret = Sub_GetValue(pSub);
}
關鍵字:子类的函數宏到父类+继承父类结构体+初始话子类时候给父类结构体值。。。。
本文介绍了一种使用C语言实现类继承的方法,通过结构体和函数指针模拟面向对象编程中的继承特性,并展示了如何利用宏定义来调用父类的方法。
1539

被折叠的 条评论
为什么被折叠?



