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++(中文版)
智能指针
129
要点速记
std::shared_ptr
提供方便的手段,实现了任意资源在共享所有权语义下进
行生命周期管理的垃圾回收。
std::unique_ptr
相比,
std::shared_ptr
的尺寸通常是裸指针尺寸的两
倍,它还会带来控制块的开销,并要求原子化的引用计数操作。
默认的资源析构通过
delete
运算符进行,但同时也支持定制删除器。删除
器的型别对
std::shared_ptr
的型别没有影响。
避免使用裸指针型别的变量来创建
std::shared_ptr
指针。
条款
20
:对于类似
std::shared_ptr
但有可能空
悬的指针使用
std::weak_ptr
如果某种智能指针能够像
std::shared_ptr
一样方便(参见条款
19
),但又无须参与
管理所指涉到的对象的共享所有权的话,虽然矛盾,却挺方便。换言之,这种指针像
std::shared_ptr
那样运作,但又不影响其指涉对象的引用计数。这么一来,这种指针
就需要处理一个对
std::shared_ptr
而言不是问题的问题:其所指涉到的对象有可能
已被析构。既然称为真正的智能指针,就应该能够通过跟踪指针何时空悬,亦即判断
其所指涉到的对象已不复存在,来处理这个问题。这些正好描述了
std::weak_ptr
种智能指针。
你可能会对
std::weak_ptr
有何用武之地感觉疑惑,而在看过
std::weak_ptr
API
后可能会更加疑惑。它看上去半点也不智能。
std::weak_ptr
不能提领,也不能检查
是否为空。这是因为 ...
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

流畅的Python(第2版)

流畅的Python(第2版)

Luciano Ramalho
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787519817749