I want to use C language to implement object-oriented, which should support inheritance, polymorphism, public or private data and methods. To do this, I thought of the following methods:
/* base.h */
typedef struct Base_tag Base;
#define DEFINE_BASE_PUBLIC_DATA /* usage: base->pub_data */
void *pub_data;
#define DEFINE_BASE_PUBLIC_METHODS /* usage: base->pub_funcN() */
void (*pub_func1)(Base *);
......
/* base.c */
struct Base_tag { void *priv_data; DEFINE_BASE_PUBLIC_DATA; void (*priv_func1)(Base *);...; DEFINE_BASE_PUBLIC_METHODS;};
Base *create_base(void *datas) { Base *base=malloc(...); ...; base->priv_func1=base_priv_func1; base->pub_func1=base_pub_func1;}
static void base_priv_func1(Base *base){ ... }
static void base_pub_func1(Base *base){ ... }
......
/* derive.h: Inherited from Base */
typedef struct derive_tag Derive;
#define DEFINE_DERIVE_PUBLIC_DATA /* usage: derive->pub_data */
void *pub_data;
#define DEFINE_DERIVE_PUBLIC_METHODS /* usage: derive->pub_funcN() */
void (*pub_func1)(Derive *);
......
/* derive.c: Inherited from Base */
struct Derive_tag { Base base; void *priv_data; DEFINE_DERIVE_PUBLIC_DATA; void (*priv_func1)(Base *);...; DEFINE_DERIVE_PUBLIC_METHODS;};
Derive *create_derive(void *datas) { Derive *derive=malloc(...); ...; derive->priv_func1=derive_priv_func1; derive->pub_func1=derive_pub_func1;}
static void derive_priv_func1(Derive *derive){ ... }
static void derive_pub_func1(Derive *derive){ ... }
......
vim/ctags can not jump exactly with this method. If I use struct to instead of “DEFINE_…”, then I must call method like that: derive->vtb->pub_funcN(), which is more strenuous than derive->pub_funcN().
So any other good ways to implement object-oriented in C language?