C++中结构和类的异同比较
发布时间
阅读量:
阅读量
在C++语言体系中,结构与类具有相似性,但同时也存在若干差异。其中最为关键的差异体现在安全性方面,以下将对主要区别进行说明:
- 在默认设置下,类中的成员属性为私有访问权限,而结构体中的成员则默认为共有访问权限。
例如,在程序1中由于访问权限设置不当导致编译过程出现错误,而在程序2中则能够顺利通过编译。
//程序1
#include <stdio.h>
class Test {
int x; // x is private
};
int main()
{
Test t;
t.x = 20; // compiler error because x is private
getchar();
return 0;
}
//程序2
#include <stdio.h>
struct Test {
int x; // x is publi
全部评论 (0)
还没有任何评论哟~
