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

12.7 指向类对象的指针

“指针”的概念是通用的,因此我们可以指向可放置于内存的任何东西。例如,我们可以使用指向vector的指针,就像使用指向char的指针一样:

注意,当我们delete一个vector时,它的析构函数会被调用。例如:

当在自由空间中创建一个vector时,new运算符:

·首先为vector分配内存。

·然后,调用vector的构造函数来初始化vector;构造函数为vector的元素分配内存,并初始化这些元素。

当删除vector时,delete运算符:

·首先调用vector的析构函数;这个析构函数调用元素的析构函数(如果它们有析构函数),然后释放元素使用的内存。

·然后,释放vector使用的内存。

注意,这个过程是如何完美地递归执行的(见8.5.8节)。如果使用实际的(标准库)vector,我们还可以实现:

这里,delete p调用vector<vector<double>>的析构函数;接着,这个析构函数调用它的vector<double>元素的析构函数,所有东西都被干净利落地清理,不会留下未销毁的对象和泄漏的内存。 ...

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