【1】每个C文件都是一个伪类,除了main()函数所在的C文件。函数
【2】在C文件顶部定义该伪类的结构体,结构体内其实并无必要使用函数指针来模拟成员函数。ui
struct fifo_t { uint8_t *buf; uint32_t size; uint32_t in; uint32_t out; };
【3】NEW。spa
经过相似的函数来建立新的伪类对象/实例。销毁相似。指针
struct fifo_t * fifo_create(uint32_t size) { struct fifo_t * fifo = malloc(sizeof(struct fifo_t)); /*
do something */ return fifo; }
【4】私有函数前加static关键字code
static void * __fifo_a_private_func(struct fifo_t *fifo,int arg)
【5】头文件中只包含结构体声明和函数的声明,保护结构体内的【私有变量】不被直接读写。对象
struct fifo_t; struct fifo_t * fifo_create(uint32_t size); void fifo_destroy(struct fifo_t *fifo); /* some other functions */
【6】没有继承。
通常也不须要继承,实在须要就换C++好了。blog
【7】只有一个对象/实例的状况。其实这种状况才比较常见,能够放弃*_create()和*_destroy()函数,头文件中也只须要函数声明,这样更完全地保护私有变量。整个C文件中只有一个有static前缀的结构体实例,函数中也很明显的区分出函数内部的局部变量和本文件的全局变量(对象)。继承
struct transmit_t { int fd; /* */ }; static struct transmit_t transmit = { .fd = -1 }; int transmit_init() { transmit.fd = // some thing /* */ } void transmit_destroy() { /* */ }/* some other function*/