C语言和C++中结构体struct区别

来源:互联网 发布:mac退出客人后黑屏 编辑:程序博客网 时间:2024/06/01 22:26

 

1

C中不支持在结构体中定义函数,但是可以使用函数指针作为成员来实现某些类似C++成员函数的功能,例如

//test.c

int add(int a, int b);

typedef struct Example

{

    int a;

    int (*pFunc)(int a, int b);

}Example;   

Example example;

example.a = 10;

example.pFunc = add;              //函数指针赋值

example.pFunc(10, 20);

 

2

C中没有private  public protected 关键字,在结构体外可以随意访问,C++中struct默认访问属性为public


3

C中用定义结构体变量时必须加上struct关键字,即struct Example example; //前提是没有使用typedef

C++中则不必遵守此规定


4

未完待续。。。。。。

 

 

 

 

原创粉丝点击