C++ 11 创建和使用 unique_ptr

unique_ptr 不共享它的指针。它无法复制到其他 unique_ptr,无法通过值传递到函数,也无法用于需要副本的任何标准模板库 (STL) 算法。只能移动unique_ptr。这意味着,内存资源所有权将转移到另一 unique_ptr,并且原始 unique_ptr 不再拥有此资源。我们建议你将对象限制为由一个所有者所有,因为多个所有权会使程序逻辑变得复杂。因此,当需要智能指针用于纯 C++ 对象时,可使用 unique_ptr,而当构造 unique_ptr 时,可使用make_unique Helper 函数。

std::unique_ptr实现了独享所有权的语义。一个非空的std::unique_ptr总是拥有它所指向的资源。转移一个std::unique_ptr将会把所有权也从源指针转移给目标指针(源指针被置空)。拷贝一个std::unique_ptr将不被允许,因为如果你拷贝一个std::unique_ptr,那么拷贝结束后,这两个std::unique_ptr都会指向相同的资源,它们都认为自己拥有这块资源(所以都会企图释放)。因此std::unique_ptr是一个仅能移动(move_only)的类型。当指针析构时,它所拥有的资源也被销毁。默认情况下,资源的析构是伴随着调用std::unique_ptr内部的原始指针的delete操作的。

下图演示了两个 unique_ptr 实例之间的所有权转换。

unique_ptr 在 STL 的 <memory> 标头中定义。它与原始指针一样有效,并可用于 STL 容器。将 unique_ptr 实例添加到 STL 容器很有效,因为通过unique_ptr 的移动构造函数,不再需要进行复制操作。

以下示例演示如何创建 unique_ptr 实例并在函数之间传递这些实例。

unique_ptr<Song> SongFactory(const std::wstring& artist, const std::wstring& title)
{
    // Implicit move operation into the variable that stores the result.
    return make_unique<Song>(artist, title);
}

void MakeSongs()
{
    // Create a new unique_ptr with a new object.
    auto song = make_unique<Song>(L"Mr. Children", L"Namonaki Uta");

    // Use the unique_ptr.
    vector<wstring> titles = { song->title };

    // Move raw pointer from one unique_ptr to another.
    unique_ptr<Song> song2 = std::move(song);

    // Obtain unique_ptr from function that returns by value.
    auto song3 = SongFactory(L"Michael Jackson", L"Beat It");
}

这些示例说明了 unique_ptr 的基本特征:可移动,但不可复制。“移动”将所有权转移到新 unique_ptr 并重置旧 unique_ptr

以下示例演示如何创建 unique_ptr 实例并在向量中使用这些实例。

void SongVector()
{
    std::vector<unique_ptr<Song>> songs;

    // Create a few new unique_ptr<Song> instances
    // and add them to vector using implicit move semantics.
    songs.push_back(make_unique<Song>(L"B‘z", L"Juice"));
    songs.push_back(make_unique<Song>(L"Namie Amuro", L"Funky Town"));
    songs.push_back(make_unique<Song>(L"Kome Kome Club", L"Kimi ga Iru Dake de"));
    songs.push_back(make_unique<Song>(L"Ayumi Hamasaki", L"Poker Face"));

    // Pass by const reference when possible to avoid copying.
    for (const auto& song : songs)
    {
        std::cout << L"Artist: " << song->artist << L"   Title: " << song->title << endl;
    }
}

在 range for 循环中,注意 unique_ptr 通过引用来传递。如果你尝试通过此处的值传递,由于删除了 unique_ptr 复制构造函数,编译器将引发错误。

以下示例演示如何初始化类成员 unique_ptr

class MyClass
{
private:
    // MyClass owns the unique_ptr.
    unique_ptr<ClassFactory> factory;
public:

    // Initialize by using make_unique with ClassFactory default constructor.
    MyClass() : factory ( make_unique<ClassFactory>())
    {
    }

    void MakeClass()
    {
        factory->DoSomething();
    }
};

可使用 make_unique 将 unique_ptr 创建到数组,但无法使用 make_unique 初始化数组元素。

// Create a unique_ptr to an array of 5 integers.
auto p = std::make_unique<int[]>(5);

// Initialize the array.
for (int i = 0; i < 5; ++i)
{
    p[i] = i;
    std::cout << p[i] << std::endl;
}

举例:

    {
        //创建一个指向int的空指针
        std::unique_ptr<int> fPtr1;
        std::unique_ptr<int> fPtr2(new int(4));
        auto fPtr3 = std::make_unique<int>();

        //fPtr2释放指向对象的所有权,并且被置为nullptr
        std::cout << "fPtr2 release before:" << fPtr2.get() << std::endl;
        int *pF = fPtr2.release();
        std::cout << "fPtr2 release before:" << fPtr2.get() << " and pF value:" << *pF << std::endl;

        //所有权转移,转移后fPtr3变为空指针
        std::cout << "move before fPtr1 address:" << fPtr1.get() << " fPtr3 address:" << fPtr3.get() << std::endl;
        fPtr1 = std::move(fPtr3);
        std::cout << "move after  fPtr1 address:" << fPtr1.get() << " fPtr3 address:" << fPtr3.get() << std::endl;

        std::cout << "move before fPtr1 address:" << fPtr1.get() << std::endl;
        fPtr1.reset();
        std::cout << "move after  fPtr1 address:" << fPtr1.get() << std::endl;
    }

输出:

  fPtr2 release before:00EFB120
  fPtr2 release before:00000000 and pF value:4
  move before fPtr1 address:00000000 fPtr3 address:00EFEC60
  move after fPtr1 address:00EFEC60 fPtr3 address:00000000
  move before fPtr1 address:00EFEC60
  move after fPtr1 address:00000000

创建与释放

#include <iostream>
#include <memory>
#include <stdlib.h>

struct Foo
{
    Foo() { std::cout << "Foo::Foo\n"; }
    ~Foo() { std::cout << "Foo::~Foo\n"; }
    void bar() { std::cout << "Foo::bar\n"; }
};

void f(const Foo &)
{
    std::cout << "f(const Foo&)\n";
}

struct D
{
    void operator()(Foo* foo)
    {
        std::cout << "D operator()" << std::endl;
        delete foo;
    }
};

void TestAutoDestroy()
{
    //1. 普通的new对象.
    std::cout << "TestDestroy...................." << std::endl;
    {
        std::unique_ptr<Foo> p1(new Foo);
    }
    //2. 普通的new[]对象.
    {
        std::unique_ptr<Foo[]> p2(new Foo[4]);
    }
    //3. 自定义的deleter.
    {
        std::unique_ptr<Foo, D> p3(new Foo);
    }
}

void TestOwner()
{
    std::cout << "TestOwner...................." << std::endl;
    //1. new object.
    std::unique_ptr<Foo> p1(new Foo);  // p1 owns Foo
    if (p1) p1->bar();

    {
        std::unique_ptr<Foo> p2(std::move(p1));  // now p2 owns Foo
        f(*p2);

        p1 = std::move(p2);  // ownership returns to p1
        p2->bar();
        std::cout << "destroying p2...\n";
    }

    p1->bar();
}

void TestArrayOwner()
{
    std::cout << "TestArrayOwner...................." << std::endl;
    //1. new[] object.
    std::unique_ptr<Foo[]> p1(new Foo[4]);  // p1 owns Foo
    if (p1) p1[0].bar();

    {
        std::unique_ptr<Foo[]> p2(std::move(p1));  // now p2 owns Foo
        f(p2[0]);

        p1 = std::move(p2);  // ownership returns to p1
        p2[0].bar();
        std::cout << "destroying p2...\n";
    }

    p1[0].bar();
}

int main()
{
    TestAutoDestroy();
    TestOwner();
    TestArrayOwner();
}

输出:
TestDestroy....................
Foo::Foo
Foo::~Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::Foo
D operator()
Foo::~Foo
TestOwner....................
Foo::Foo
Foo::bar
f(const Foo&)
Foo::bar
destroying p2...
Foo::bar
Foo::~Foo
TestArrayOwner....................
Foo::Foo
Foo::Foo
Foo::Foo
Foo::Foo
Foo::bar
f(const Foo&)
Foo::bar
destroying p2...
Foo::bar
Foo::~Foo
Foo::~Foo
Foo::~Foo
Foo::~Foo
 
时间: 2024-08-27 07:06:35

C++ 11 创建和使用 unique_ptr的相关文章

使用Django1.11创建简单的资产管理平台

1:首先创建一个django项目 [[email protected] opt]# django-admin startproject opsCommandError: '/opt/ops' already exists[[email protected] opt]# cd ops[[email protected] ops]# tree.├── manage.py└── ops    ├── __init__.py    ├── settings.py    ├── urls.py    └─

Windows server2008/2012 安装oracle 11 创建实例HANG住在百分之2

Windows server2008/2012 安装oracle 11.2.0.1的时候,可能会在创建数据库实例的时候卡在百分之2的地方. 这个时候可以 1.点击开始菜单,在“搜索程序和文件”中输入“msconfig”,回车即打开“系统配置”对话框, 请在此对话框中切换到“引导”标签,然后单击图中红圈处的“高级选项”按钮,弹出引导高级选项对话框. 2.在弹出的“引导高级选项”对话框中勾选处理器数,建议修改为系统最大核数的一半. 3.修改完毕后,点击“确定”按钮,保存设置退出即可. 4.然后重新配

C++11智能指针:unique_ptr

unique_ptr 1.概念 unique_ptr形如其名,与所指对象的内存紧密地绑定,不能与其他的unique_ptr类型的指针对象共享所指向对象的内存. 在cplusplus.com中,unique_ptr声明如下: // non-specialized template <class T, class D = default_delete<T>> class unique_ptr; // array specialization template <class T,

C++ 11 创建和使用共享 weak_ptr

有时对象必须存储一种方法,用来在不引起引用计数增加的情况下访问 shared_ptr 的基础对象.通常,当您在 shared_ptr 实例之间循环引用时,就会出现此情况. 最佳的设计能够尽可能地避免指针具有共享所有权.但是,如果您必须具有共享的 shared_ptr 实例所有权,请避免在实例之间进行循环引用.如果循环引用不可避免,甚至由于某种原因而更为可取,请使用 weak_ptr 为一个或多个所有者提供对其他 shared_ptr 的弱引用.使用 weak_ptr,您可以创建连接到现有相关实例

C++ 11 创建和使用 shared_ptr

shared_ptr 的类型是C + +标准库中一个聪明的指针,是为多个拥有者管理内存中对象的生命周期而设计的.在你初始化一个 shared_ptr 后,你可以复制它,把函数参数的值递给它,并把它分配给其它 shared_ptr 实例.所有实例指向同一个对象,并共享访问一个“控制块”,即每当一个新的shared_ptr 被添加时,递增和递减引用计数,超出范围,则复位.当引用计数到达零时,控制块删除内存资源和自身. 下图显示了指向一个内存位置的几个 shared_ptr 实例. 无论什么时候,当内

Powershell AWS 自动化管理 (11) - 创建一个高可用的WordPress博客(中)

理论和基本架构在上一篇已经做了说明,这一篇直接来看看具体的脚本实现吧.首先来看看前面10个步骤的实现. 创建EC2-S3的Role,这个Role是分配给EC2虚拟机的,这样他们创建之后自动就有权限访问S3的内容. 创建VPC网络 创建VPC的2个子网,位于不同的AZ 创建Internet网关 配置路由表 创建并配置EC2的Security Group,确保80和22端口可用 创建高可用的MariaDB数据库 配置数据库的Security Group,确保3306端口可用 创建S3 Bucket

Windows Azure Virtual Network (11) 创建VNet-to-VNet的连接

<Windows Azure Platform 系列文章目录> 我们知道,Azure Virtual Network可以 1.将对台Azure VM加入到同一个网段里,同时绑定内网IP地址 2.创建VPN网关,来设置Site-To-Site VPN或者Point-To-Site VPN 但是我们在使用Virtual Network的时候,会遇到这样的情况: 1.之前创建的2个Virtual Network,本来是互相不能通过内网访问的.但是后期想通过内网互通互联. 2.在创建的Virtual

QC ALM 11创建域、项目和用户

一旦HP-ALM安装,我们仅仅能继续创建域.项目和用户使用后的ALM工作.以下是步骤来创建项目.域和用户.       一.创建域 1.对于创建域,第一步是进入站点管理员页面.开展QC使用URL - http://localhost:8080/qcbin/SiteAdmin.jsp. 并输入被设置在安装QC管理员凭据.例如以下图所看到的:   2.登录到站点后台管理页面后.我们会看到站点项目登陆标签,例如以下图所看到的.下一步是创建一个域. 3.点击在左上角的'创建域'button,然后输入域名

c++11 智能指针 unique_ptr、shared_ptr与weak_ptr

c++11 智能指针 unique_ptr.shared_ptr与weak_ptr C++11中有unique_ptr.shared_ptr与weak_ptr等智能指针(smart pointer),定义在<memory>中. 可以对动态资源进行管理,保证任何情况下,已构造的对象最终会销毁,即它的析构函数最终会被调用. unique_ptr unique_ptr持有对对象的独有权,同一时刻只能有一个unique_ptr指向给定对象(通过禁止拷贝语义.只有移动语义来实现). unique_ptr