Skip to Content
介绍 C++ (Chinese Edition)
book

介绍 C++ (Chinese Edition)

by Frances Buontempo
March 2026
Intermediate
348 pages
3h 59m
Chinese
O'Reilly Media, Inc.
Content preview from 介绍 C++ (Chinese Edition)

第 12 章. 使用 std::unique_ptr进行内存 管理

本章将向您展示如何有效地处理堆上的对象,以确保在不再需要时释放内存。 您在第 9 章中接触过char指针,并在第 11 章中详细了解了std::stringstd::string提供了比char指针更高的抽象层次,使您的编程更加轻松。 与std::vector 一样,std::string会自动调整大小,且两者在作用域结束时都会自动清理,因此您无需亲自动手。 C++ 还提供了其他功能,帮助您在对象使用完毕后进行清理。 如果你确实希望清理那些通过 `` 在堆上分配的对象,可以使用智能指针来避免低级的手动内存管理:这是一种像普通指针一样工作,但会为你自动清理的类型。 智能指针有多种类型,我们先从最容易使用的开始。 随后,你将能够在第 13 章中使用智能指针来扩展你的交易游戏。

那么,为什么要使用堆? 到目前为止,你还没有必要直接自己处理这些,尽管std::stringstd::vector可能在后台使用了堆。 第 13 章将向你展示堆对象的一个重要用例:允许行为随类类型而变化。 另一个用例是针对大小可变的对象。 许多对象具有固定大小,例如整数。 相比之下,std::vectorstd::string都可以包含数量可变的元素,因此当你无法在编译时确定所需大小时,请使用 通过堆进行动态分配。

本章将向您展示如何以一种既能确保代码安全又巧妙的方式处理动态内存指针。 您将深入了解构造函数,并学习一种在编译时测试代码的新方法。 您还将复习引用,读完本章后,您将准备好在下一章编写一个改进版的交易游戏。 自第 10 章以来,您一直在学习如何使用构建块来创建、移动和复制类,本章将帮助您做好准备,利用类来改变行为。

创建 std::unique_ptr

<memory>头文件提供了多种智能指针及其创建函数。 本节将向您展示如何使用std::unique_ptr,随后“智能指针深入解析”部分将简要概述其他类型的智能指针。 智能指针比原始指针更易于使用,因为它们会为您处理内存管理。 当您获取堆内存时,应在使用完毕后将其释放。否则,程序将持续占用该内存,这可能导致系统内存耗尽。 您还可以使用智能指针来管理其他资源。

std::unique_ptrstd::make_unique 配合使用。 std::make_unique 函数会为你调用new,而智能指针的析构函数默认会调用delete。 (我在“std::string 是如何工作的?”一节中曾向你介绍过这些函数) 如果你使用智能指针,就无需直接处理这些函数。

创建一个新的main.cpp文件。 包含memory头文件和你的Stock头文件:

#include <memory> 1

#include "Stock.h" 2

int main()
{
    using 命名空间 stock_prices; 
    auto 资产{ 
        std::make_unique<库存>("咖啡", 4.8, 0.0113) 
    };
}

包含内存

包含您的股票 ...

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

Navigating the midcareer plateau

Navigating the midcareer plateau

Uma Chingunde
Run Llama-2 Models

Run Llama-2 Models

Federico Castanedo

Publisher Resources

ISBN: 0642572352684