关于c和c++中的struct类型
来源:互联网 发布:久量台灯怎么样知乎 编辑:程序博客网 时间:2024/06/08 02:22
c 语言中的struct类型和c++中的class很类似,那么它们你之间到底有什么区别呢?
c语言中的struct类型在默认情况下是public,而class在默认情况下却是private类型。不知你曾经是否思考过关于在struct中是否可以和class中能否定义函数的问题。
struct是不能定义函数的,应为函数不是一个对象。在c99中Rationale精辟的指出所有c中对象的 本质:
All objects in c must be representalbe as a contiguous sequence of bytes!
尽管如此,但是我们却可以在struct中定义一个函数指针,如:
int add(int a,int b)
{
return (a+b);
}
struct xy
{
int (*lx)(int ,int );
} ;
int main()
{ int res;
struct xy xy_a;
xy_a . lx=add;
a=xy_a.lx(2,3);
cout<<"The result is:/n"<<a;
return 0;
}
但是正是由于它的这种定义方式,也使它变得不安全!所以应该在c中尽量不要使用这种所谓的“面向对象”的特性。
c语言中的struct类型在默认情况下是public,而class在默认情况下却是private类型。不知你曾经是否思考过关于在struct中是否可以和class中能否定义函数的问题。
struct是不能定义函数的,应为函数不是一个对象。在c99中Rationale精辟的指出所有c中对象的 本质:
All objects in c must be representalbe as a contiguous sequence of bytes!
尽管如此,但是我们却可以在struct中定义一个函数指针,如:
int add(int a,int b)
{
return (a+b);
}
struct xy
{
int (*lx)(int ,int );
} ;
int main()
{ int res;
struct xy xy_a;
xy_a . lx=add;
a=xy_a.lx(2,3);
cout<<"The result is:/n"<<a;
return 0;
}
但是正是由于它的这种定义方式,也使它变得不安全!所以应该在c中尽量不要使用这种所谓的“面向对象”的特性。
- 关于c和c++中的struct类型
- C语言中的struct和typedef struct
- c/c++ 中的 struct和typedef struct
- C++struct结构类型
- C++和C中的struct和class
- c 和 c++中的 struct 和 class
- typedef struct和 struct 在C和C++中的异同
- struct和typedef struct,在C和C++中的区别
- struct和typedef struct在c++和c中的区别
- 关于 C语言中typedef struct 和 struct
- C和C++中的struct的区别
- C++和C中的struct区别
- struct 在C++和C中的区别
- struct在C和C++中的区别
- C/C++中的typedef struct 和 struct区别
- C#中struct和C中的struct的一个区别
- 认识C中的Struct
- C/C++中的struct
- 一些JAVA下载链接
- Socket与拔掉网线
- WAP常见问题问答大全
- 克服孤独和茫然
- Few Welcome Mats for Smokers
- 关于c和c++中的struct类型
- 转 代码外观
- 开通我的技术博客,和以前关于生活的博客,分开旅行
- 在ASP.NET 2.0中,一个ASP.NET页面的生命周期
- ASP.NET中文件上传下载方法集合
- JBPM-Decision的用法
- With Fear and Wonder in Its Wake, Sputnik Lifted Us Into the Future
- 在VB中调用SAP function及SAP RFC的例子
- IIS积累