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++(中文版)
113
4
智能指针
诗人和作曲家往往对爱情情有独钟,有时又对计数偏爱有加,偶然也会把两者兼收并
蓄。伊丽莎白
·
巴雷特
·
勃朗宁(
Elizabeth Barrett Browning
)有诗云,“何以爱汝,待
我细数”(
How do I love thee? Let me count the ways
),保罗
·
西蒙(
Paul Simon
亦有绝唱,“逃离爱娇娃,必有五十法”(
There must be 50 ways to leave your
lover
),受此启发,我们也来把对于裸指针实在爱不起来的理由一一列举。
1.
裸指针在声明中并没有指出,裸指针指涉到的是单个对象还是一个数组。
2.
裸指针在声明中也没有提示在使用完指涉的对象以后,是否需要析构它。换言之,
你从声明中看不出来指针是否拥有(
own
)其指涉的对象。
3.
即使知道需要析构指针所指涉的对象,也不可能知道如何析构才是适当的。是应
该使用
delete
运算符呢,还是别有它途(例如,可能需要把指针传入一个专门的、
用于析构的函数)?
4.
即使知道了应该使用
delete
运算符,参见理由
1
,还是会发生到底应该用单个对
象形式(“
delete
”)还是数组形式(“
delete[]
”)的疑问。一旦用错,就会导
致未定义行为。
5.
即启用够确信,指针拥有其指涉的对象,并且也确知应该如何析构,要保证析构
在所有代码路径上都仅执行一次(包括那些异常导致的路径)仍然困难重重。只
要少在一条路径上执行,就会导致资源泄漏。而如果析构在一条路径上执行了多
于一次,则会导致未定义行为。 ...
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