C语言实现结构体继承

#include 
#include 
#include 

#ifndef VIRTUAL
#define VIRTUAL
#endif

#ifndef DELETE
#define DELETE(X)	do { free(X);X = NULL; } while(0)
#endif

#define NEW(TYPE,pInstance,SUBTYPE)	struct TYPE* pInstance = NULL;	\
									struct SUBTYPE* SUBTYPE##instance = (struct SUBTYPE*)malloc(sizeof(struct SUBTYPE));	\
									SUBTYPE##instance->SUBTYPE##_constructor = SUBTYPE##_constructor;	\
									SUBTYPE##instance->SUBTYPE##_constructor(SUBTYPE##instance);	\
							pInstance = (struct TYPE*)(SUBTYPE##instance); /* pInstance points to base class and sub class */

#define container_of(ptr, type, member) ({ \
					const typeof( ((type *)0)->member ) *__mptr = (ptr); \
					(type *)( (char *)__mptr - offsetof(type,member) );})

enum Authority
{
	eUser = 0,
	eAdmin,
	eEngineer
};

struct BASE /* base class */
{	
	void (*BASE_constructor)(struct BASE*);
	VIRTUAL void (*access)(struct BASE*);
};

struct USER /* sub class */
{
	struct BASE m_nParent;
	/* data segment */
	char m_arrData[16];
	void (*USER_constructor)(struct USER*);
};

struct ADMIN /* sub class */
{
	struct BASE m_nParent;
	/* data segment */
	char m_arrData[16];
	void (*ADMIN_constructor)(struct ADMIN*);
};

struct ENGINEER /* sub class */
{
	struct BASE m_nParent;
	/* data segment */
	char m_arrData[16];
	void (*ENGINEER_constructor)(struct ENGINEER*);
};

void BASE_constructor(struct BASE* pThis)
{
	printf("BASE_constructor\n");
}

void USER_access(struct BASE* pThis)
{
	/* to get sub class */
	struct USER* pTh = (struct USER*)pThis;
	printf("USER_access : %s\n",pTh->m_arrData);
}

void USER_constructor(struct USER* pThis)
{	
	(pThis->m_nParent.BASE_constructor = BASE_constructor)(&(pThis->m_nParent));
	strcpy(pThis->m_arrData,"User");
	pThis->m_nParent.access = USER_access;
}

void ADMIN_access(struct BASE* pThis)
{
	/* to get sub class */
	struct ADMIN* pTh = (struct ADMIN*)pThis;
	printf("ADMIN_access : %s\n",pTh->m_arrData);
}

void ADMIN_constructor(struct ADMIN* pThis)
{
	(pThis->m_nParent.BASE_constructor = BASE_constructor)(&(pThis->m_nParent));
	strcpy(pThis->m_arrData,"Admin");
	pThis->m_nParent.access = ADMIN_access;
}

void ENGINEER_access(struct BASE* pThis)
{
	/* to get sub class */
	struct ENGINEER* pTh = (struct ENGINEER*)pThis;
	printf("ENGINEER_access : %s\n",pTh->m_arrData);
}

void ENGINEER_constructor(struct ENGINEER* pThis)
{
	(pThis->m_nParent.BASE_constructor = BASE_constructor)(&(pThis->m_nParent));
	strcpy(pThis->m_arrData,"Engineer");
	pThis->m_nParent.access = ENGINEER_access;
}

struct BASE* getUser(void)
{
	NEW(BASE,pBase,USER);
	return pBase;
}

struct BASE* getAdmin(void)
{
	NEW(BASE,pBase,ADMIN);
	return pBase;
}

struct BASE* getEngineer(void)
{
	NEW(BASE,pBase,ENGINEER);
	return pBase;
}

struct BASE* getInstance(enum Authority e)
{
	struct BASE* pBase = NULL;

	switch(e)
	{
		case eUser:		pBase = getUser() ; break;
		case eAdmin:	pBase = getAdmin() ; break;
		case eEngineer:	pBase = getEngineer() ; break;
		default: 		printf("error enum!\n");
	}

	return pBase;
}

int main(void)
{
	struct BASE* pBase = getInstance(eAdmin);

	pBase->access(pBase);

	DELETE(pBase);

	return 0;
}

输出:

BASE_constructor
ADMIN_access : Admin


你可能感兴趣的:(c)