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版)

14.5.2 资源获取即初始化

幸运的是,我们可以不必在代码中添加复杂的try…catch语句就能有效处理潜在的资源泄漏问题。例如:

这一实现就好多了。更重要的是,它显然好得多。资源(在这里是自由存储区中的内存空间)由构造函数获取,而由对应的析构函数释放。当解决了向量的内存泄漏问题之后,我们实际上已经解决了这类特别的“异常问题”。这一解决方法具有一般性;它能用于所有资源类型:通过对象的构造函数获取资源,并通过对应的析构函数释放资源。通过这一方法能够有效处理的资源包括:数据库锁、套接字和I/O缓冲区。这一技术有一个拗口的名字“Resource Acquisition Is Initialization”——资源获取即初始化,简写为RAII。

再回到上面的例子。不论我们采用哪种方式退出函数f(),p和q的析构函数都将被正常调用:因为p和q不是指针,我们不能对它们赋值,return语句和异常的抛出均不会妨碍析构函数的执行。当程序的执行序列超出了被完全构造的对象或子对象的作用域时,这些对象的析构函数将自动被调用。对一个对象而言,当其构造函数执行完毕时,它被认为构造完成。探寻这两句话的详细含义是一个让人头疼的事情,但它们的基本含义是对象的构造函数和析构函数会根据实际需要被调用。

特别地,当我们需要在某个作用域内使用可变大小的存储空间时,我们应使用vector而不是显式使用new和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版)

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

管理Kubernetes

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

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano

Publisher Resources

ISBN: 9787111562252