Skip to Content
C++程序设计:原理与实践(基础篇)(原书第2版)
book

C++程序设计:原理与实践(基础篇)(原书第2版)

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
9h 36m
Chinese
Pearson
Content preview from C++程序设计:原理与实践(基础篇)(原书第2版)

3.8 类型和对象

类型是C++和大多数编程语言的核心内容。接下来我们以更技术性的观点更近距离地讨论类型,特别是我们在计算过程中用来存储数据的对象类型。长远来看这会节省你的时间,它也可以避免引起你的混淆。

·类型定义一组可能的值与一组运算(对于一个对象)。

·对象是用来保存一个指定类型值的一些内存单元。

·值是根据一个类型来解释的内存中的一组比特。

·变量是一个命名的对象。

·声明是命名一个对象的一条语句。

·定义是一个声明,但同时也为对象分配了内存空间。

通俗地说,我们可以将一个对象看作一个盒子,我们可以将指定类型的值放入它。一个int盒子可以保存整数,例如7、42与-399。一个string盒子可以保存字符串值,例如“Interoperability”、“tokens:!@#$%^&*”与“Old MacDonald had a farm”。我们可以用图表来考虑:

由于string要跟踪它保存的字符数,因此string比int的表示方法更复杂。注意,一个double保存一个数字,而一个string保存的是字符序列。例如,x保存数字1.2,而s2保存三个字符'1'、'.'与'2'。字符和字符串常量的引号并不保存。

每个int的大小是相同的;也就是说,编译器为每个int分配相同的固定大小的内存。在一个典型的台式计算机中,这个大小是4个字节(32个比特)。与此类似,bool、char与double是固定大小的。在通常情况下,你会发现台式计算机为一个bool或char分配1个字节(8个比特),为一个double分配8个字节。注意,不同类型的对象使用不同大小的空间。特别地,一个char比一个int占用更少的空间,string不同于double、int与char,不同大小的字符串占用不同大小的空间。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252