Skip to Content
Effective Modern C++(中文版)
book

Effective Modern C++(中文版)

by Scott Meyers
April 2018
Intermediate to advanced
303 pages
4h 54m
Chinese
China Electric Power Press Ltd.
Content preview from Effective Modern C++(中文版)
120
4
std::array
std::vector
std::string
这些数据结构几乎总是比裸数组更好。我
能想到的唯一
std::unique_ptr<T[]>
的合理使用场景是使用了一个
C
风格的
API
它返回了堆上的裸指针,且指定了其指涉对象的所有权。
std::unique_ptr
C++11
中表达专属所有权的方式,但它还有一个十分吸引人的特
性,就是
std::unique_ptr
可以方便高效地转换成
std::shared_ptr
std::shared_ptr<Investment>
sp =
//
std::unique_ptr
型别的对象
makeInvestment( arguments );
//
转换为
std::shared_ptr
型别
正是这一特性使得
std::unique_ptr
如此适合作为工厂函数的返回型别。工厂函数
并不知道调用者是对其返回的对象采取专属所有权语义好,还是共享所有权(例如,
std::shared_ptr
)更合适。通过返回一个
std::unique_ptr
,工厂函数就向调用者提
供了最高效的智能指针,但它也不会阻止调用者把返回值转换成更具弹性的其他兄弟
型别的智能指针(关于
std::shared_ptr
请参考条款
19
)。
要点速记
std::unique_ptr
是小巧、高速的、具备只移型别的智能指针,对托管资源
实施专属所有权语义。
默认地,资源析构采用
delete
运算符来实现,但可以指定自定义删除器。
有状态的删除器和采用函数指针实现的删除器会增加 ...
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版)

本贾尼 斯特劳斯特鲁普
C++语言导学(原书第2版)

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

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749