原先对C中的struct理解只局限在数据结构体上,看了部分linux内核代码发现struct用处很多,功能很强大,可以在C环境下实现几乎所有C++中class的用处,我写了一个很简单的一个例子:
#include<stdio.h> struct MyClass { char* name; int age; void (*funnull) (); void (*func) (struct MyClass mc); }; void realfunnull() { printf("hello world!/n"); } void realfunc(struct MyClass mc) { printf("MyClass's name is:%s/n",mc.name); printf("MyClass's age is:%d/n",mc.age); } int main() { struct MyClass mc = {"Simon", 25, realfunnull, realfunc}; mc.funnull(); mc.func(mc); return 0; } |
在C中用struct实现C++中class的函数声明和定义
最新推荐文章于 2024-06-26 08:38:00 发布