我今天由于业务需要 看了一下嵌入式的代码,看完之后我笑了。如果这段代码放到嵌入式的板块他们估计都会不感觉有什么稀奇的,放这里大家会感觉有点意思。
extern int fun(int x);
extern void fun1(int y);typedef struct classa {
int (*method)(int x);
void (*method1)(int y);} hello;
int main(int argc, char* argv[])
{
hello*  a;
a = (hello*)malloc(sizeof(hello));
a->method = fun;
a->method1 = fun1;
int w = a->method(2);
a->method1(3);

return 0;
}
int fun(int x)
{
cout<<"methond"<<endl;
return x;
}
void fun1(int y)
{
cout<<"methond1"<<endl;
}
以上这段代码是我在对那个代码做的抽象一下,extern int fun(int x);
extern void fun1(int y);typedef struct classa {
int (*method)(int x);
void (*method1)(int y);} hello;
             跟这一段跟c++里的 
class classa 
{public:
  int method(int x);
  void method1(int y);
  
}
  做对比
接下来         hello*  a;
a = (hello*)malloc(sizeof(hello));
a->method = fun;
a->method1 = fun1;
 这一段跟c++里的
 hello *a =new hello;
这一句做对比(其中这里a = (hello*)malloc(sizeof(hello));
a->method = fun;
a->method1 = fun1;
这3句全被隐藏的class hello 构造函数完成了
)
 
        int w = a->method(2);
        a->method1(3);与 c++中的
 a->method(2);
 a->method(3);
 其中 a 使这个东西   hello *a =new hello;对比接下来
int fun(int x)
{
cout<<"methond"<<endl;
return x;
}
void fun1(int y)
{
cout<<"methond1"<<endl;
}
 这段代码跟
 int hello::fun(int x)
{};
void hello::fun1(int y)
{};
全能在功能上对应上吧,一个面向结构语言c完全实现了类和对象这个概念。
这个说明啥?  呵呵。