Skip to Content
C++语言导学(原书第2版)
book

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

by 本贾尼 斯特劳斯特鲁普
August 2021
Intermediate to advanced
450 pages
5h 35m
Chinese
Pearson
Content preview from C++语言导学(原书第2版)

4.2 具体类型

具体类(concrete class)的基本思想是,它们的行为“就像内置类型一样”。例如,一个复数类型和一个无穷精度整数与内置的int非常相像,当然它们有自己的语义和操作集。同样,vector和string也很像内置的数组,只不过它们的行为更加良好(参见9.2节、10.3节、11.2节)。

具体类型的典型特征是,其表示是定义的一部分。在很多重要的例子中,如vector,其表示只是一个或几个指针,指向保存在别处的数据,但这种表示出现在具体类的每一个对象中。这令实现可以在时空上达到最优。特别是,它允许我们

·将具体类型的对象置于栈、静态分配的内存或者其他对象中(参见1.5节);

·直接引用对象(而非仅仅通过指针或引用);

·立即进行完整的对象初始化(比如使用构造函数,参见2.3节);

·拷贝和移动对象(参见5.2节)。

类的表示可以是私有的(就像Vector一样,参见2.3节)从而只能通过成员函数访问,但它确实是存在的。因此,如果表示方式发生了任何明显的改动,使用者就必须重新编译。这就是我们令具体类型的行为与内置类型完全一样需要付出的代价。对于某些场景,不常改动的类型和局部变量提供了迫切需要的清晰性和效率,此时这种特性是可以接受的,而且通常很理想。为了提高灵活性,具体类型可以将其表示的主要部分放置在自由存储(动态内存、堆)中,然后通过存储在类对象内部的成员访问它们。vector和string就是这样实现的,我们可以将它们看成带有精心打造的接口的资源管理器。

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

软件开发实践:项目驱动式的Java开发指南

软件开发实践:项目驱动式的Java开发指南

Raoul-Gabriel Urma, Richard Warburton
管理Kubernetes

管理Kubernetes

Brendan Burns, Craig Tracey
Python机器学习案例精解

Python机器学习案例精解

Posts & Telecom Press, Yuxi (Hayden) Liu

Publisher Resources

ISBN: 9787111633280