在C++中,除了以下几点外,struct和class是相同的。

  (1)class的成员的默认访问控制是private,而struct的成员的默认访问权限是public。

  例如,program 1会编译失败,而program 2可正常运行。

 1 // Program 1
2 #include <stdio.h>
3
4 class Test
5 {
6 int x; // x is private
7 };
8
9 int main()
10 {
11 Test t;
12 t.x = 20; // compiler error because x is private
13 getchar();
14 return 0;
15 }
16
17 // Program 2
18 #include <stdio.h>
19
20 struct Test
21 {
22 int x; // x is public
23 };
24
25 int main()
26 {
27 Test t;
28 t.x = 20; // works fine because x is public
29 getchar();
30 return 0;
31 }

  (2)在继承体系中,当struct继承自struct/class时,对于base struct/class的默认访问控制是public。但当class继承自struct/class时,对base struct/class的默认访问控制是private。

  例如,program 3会编译失败,而program 4可正常运行。

 1 // Program 3
2 #include <stdio.h>
3
4 class Base
5 {
6 public:
7 int x;
8 };
9
10 class Derived : Base
11 {
12 }; // is equilalent to class Derived : private Base {}
13
14 int main()
15 {
16 Derived d;
17 d.x = 20; // compiler error becuase inheritance is private
18 getchar();
19 return 0;
20 }
21
22 // Program 4
23 #include <stdio.h>
24
25 class Base
26 {
27 public:
28 int x;
29 };
30
31 struct Derived : Base
32 {
33 }; // is equilalent to struct Derived : public Base {}
34
35 int main()
36 {
37 Derived d;
38 d.x = 20; // works fine becuase inheritance is public
39 getchar();
40 return 0;
41 }

  总结:

  对于struct和class的不同点就在于:默认访问控制不同,struct为public,class为private。

  而这个不同体现在两个地方:一是成员的默认访问控制;二是继承体系中对基类的默认访问控制。

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-25  19:52:01

struct vs class in C++的更多相关文章

  1. 使用struct处理二进制

    有的时候需要用python处理二进制数据,比如,存取文件.socket操作时.这时候,可以使用python的struct模块来完成. struct模块中最重要的三个函数是pack(), unpack( ...

  2. golang struct扩展函数参数命名警告

    今天在使用VSCode编写golang代码时,定义一个struct,扩展几个方法,如下: package storage import ( "fmt" "github.c ...

  3. go-使用 unsafe 修改 struct 中的 field 的值

    以下是方法,不要纠结原理,等东西积累多了,你才有能力纠结原理: 首先,你需要有一个这样的函数,这是在 nsq 的源码里直接抄过来的: func unsafeValueOf(val reflect.Va ...

  4. C语言中struct位域的定义和使用

    位域的定义和使用 有些信息在存储时,并不需要占用一个完整的字节, 而只需占几个或一个二进制位.例如在存放一个开关量时,只有0和1 两种状态, 用一位二进位即可.为了节省存储空间,并使处理简便,C语言又 ...

  5. C# Struct结构体里数组长度的指定

    typedef struct Point{ unsigned short x; unsigned short y; }mPoint;//点坐标 typedef struct Line{ mPoint ...

  6. C 语言Struct 实现运行类型识别 RTTI

    通过RTTI,能够通过基类的指针或引用来检索其所指对象的实际类型.c++通过下面两个操作符提供RTTI. (1)typeid:返回指针或引用所指对象的实际类型.    (2)dynamic_cast: ...

  7. VC++ : error LNK2001: unresolved external symbol "__declspec(dllimport) public: __thiscall std::basic_string<wchar_t,struct std::char_traits<wchar_t>

    最近学习Google Breakpad,将其用在了自己的项目中,编译的版本为VS2010,没有什么问题.但是为了和之前的程序兼容,需要使用VS2008版本的程序,于是又编译了VS2008版本的代码,但 ...

  8. 字节流与数据类型的相互转换---使用struct模块

    字节流与数据类型的相互转换---使用struct模块 http://blog.csdn.net/Sunboy_2050/article/details/5974029 Python是一门非常简洁的语言 ...

  9. 窥探Swift之别具一格的Struct和Class

    说到结构体和类,还是那句话,只要是接触过编程的小伙伴们对这两者并不陌生.但在Swift中的Struct和Class也有着令人眼前一亮的特性.Struct的功能变得更为强大,Class变的更为灵活.St ...

  10. struct 大小计算

    结构体是一种复合数据类型,通常编译器会自动的进行其成员变量的对齐,已提高数据存取的效率.在默认情况下,编译器为结构体的成员按照自然对齐(natural alignment)条方式分配存储空间,各个成员 ...

随机推荐

  1. 攻防世界 Misc 新手练习区 ext3 bugku Writeup

    攻防世界 Misc 新手练习区 ext3 bugku Writeup 题目介绍 题目考点 WinHex工具的使用 linux磁盘挂载mount命令 Writeup 下载附件拖进winhex分析一下,查 ...

  2. 远程连接linux | Xshell和Xftp下载安装

    为什么需要远程登录linux 公司开发时候, 具体的情况是这样的: Linux 一般作为服务器使用,而服务器一般放在机房,你不可能在机房操作你的 Linux 服务器.这时我们就需要远程登录到Linux ...

  3. MnogoDB唯一索引,稀疏索引

    1,单个字段唯一索引 db.collection.createIndex({name:1},{unique:true} 2,多个字段联合索引示例 db.collection.createIndex({ ...

  4. maven添加代理,默认的.m2路径

    Maven设置http代理 编辑~/.m2/settings.xml文件,添加如下配置 找到 <proxies> 节点.去掉相应的注释,设置代理信息如下: 1 <proxy> ...

  5. 简单的SQl时间序列生成,每次时间间隔10分钟。

    create table #timeseries(Times datetime not null) go declare @firstdate datetime , @lastdate datetim ...

  6. Python基础(作用域)

    def _private_1(name): return 'Hello, %s' % name def _private_2(name): return 'Hi, %s' % name def gre ...

  7. GIS应用|快速开发REST空间分析服务

    随着计算机的快速发展,GIS已经在各大领域得到应用,和我们的生活息息相关, 但是基于GIS几大厂商搭建服务,都会有一定的门槛,尤其是需要server,成本高,难度大,这里介绍一种在线GIS云平台,帮你 ...

  8. [loj2393]门票安排

    为了方便,不妨假设$a_{i}\le b_{i}$,并将问题转换为以下形式: $\forall 1\le i\le m$,将$[a_{i},b_{i})$或$[1,a_{i})\cup [b_{i}, ...

  9. [atARC058F]Lroha Loves Strings

    贪心,求出前$i$个字符串所能组成的字典序最小的字符串$ans$(特别的,这里的字典序有$ab>abc$),同时保证剩下的长度能通过$l_{i+1},...,l_{n}$拼接 考虑插入一个字符串 ...

  10. /dev/random 和 /dev/urandom 的原理

    /dev/null 是一个特殊的设备文件,它丢弃一切写入其中的数据 可以将它 视为一个黑洞, 它等效于只写文件, 写入其中的所有内容都会消失, 尝试从中读取或输出不会有任何结果,同样,/dev/nul ...