C中struct的成员只能是变量,不能有函数。
C++的struct成员可以有函数。 C++中的成员默认是public的,而class中的成员默认是private的。
那么怎样用struct来实现C++中的class呢?,可以通过拥有函数的指针来调用函数实现,例如以下。
#include <stdio.h> typedef struct ptest { void (*pFunction)(); }STest; void display() { printf("hello function\n"); } void main(void) { STest test; test.pFunction = display; test.pFunction(); }