8、shared_ptr

  共享智能指针是指多个智能指针可以同时管理同一块有效的内存,共享智能指针 shared_ptr 是一个模板类

一、shared_ptr 的初始化

  (1)通过构造函数

std::shared_ptr<T> 智能指针名字(创建堆内存);
#include "stdafx.h"

#include <iostream>
#include <windows.h>
#include <functional>
#include <memory>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    //使用智能指针管理一块int型内存
    shared_ptr<int> ptr1(new int(520));
    cout << "ptr1管理的内存引用计数:" << ptr1.use_count() << endl;
    //使用智能指针管理一块字符数组对应的堆内存
    shared_ptr<char> ptr2(new char[10]);
    cout << "ptr2管理的内存引用计数:" << ptr2.use_count() << endl;
    //创建智能指针对象,不管理任何内存
    shared_ptr<int> ptr3;
    cout << "ptr3管理的内存引用计数:" << ptr3.use_count() << endl;
    shared_ptr<int> ptr4(nullptr);
    cout << "ptr4管理的内存引用计数:" << ptr4.use_count() << endl;

    system("pause");
    return 0;
}

注意:

  如果智能指针被初始化了一块有效内存,那么这块内存的引用计数 + 1,如果智能指针没有被初始化或者被初始化为 nullptr 空指针,引用计数不会 + 1。另外,不要使用一个原始指针初始化多个 shared_ptr。

  (2)通过拷贝和移动构造函数初始化

#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <memory>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    shared_ptr<int> ptr1(new int(520));

    //调用拷贝构造函数
    shared_ptr<int> ptr2(ptr1);
    
    //调用移动构造函数
    shared_ptr<int>    ptr3(std::move(ptr1));

    system("pause");
    return 0;
}

  (3)std::make_shared辅助函数

    通过 C++ 提供的 std::make_shared() 就可以完成内存对象的创建并将其初始化给智能指针,函数原型如下:

template< class T, class... Args >
shared_ptr<T> make_shared( Args&&... args );
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <memory>

using namespace std;

class Test
{
public:
    Test()
    {
        cout << "construct Test..." << endl;
    }
    Test(int x)
    {
        cout << "construct Test, x = " << x << endl;
    }
    Test(string str)
    {
        cout << "construct Test, str = " << str << endl;
    }
    ~Test()
    {
        cout << "destruct Test ..." << endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    shared_ptr<int> ptr1 = make_shared<int>(520);
    cout << "ptr1管理的内存引用计数: " << ptr1.use_count() << endl;

    shared_ptr<Test> ptr2 = make_shared<Test>();
    cout << "ptr2管理的内存引用计数: " << ptr2.use_count() << endl;

    shared_ptr<Test> ptr3 = make_shared<Test>(520);
    cout << "ptr3管理的内存引用计数: " << ptr3.use_count() << endl;

    shared_ptr<Test> ptr4 = make_shared<Test>("我是要成为海贼王的男人!!!");
    cout << "ptr4管理的内存引用计数: " << ptr4.use_count() << endl;


    system("pause");
    return 0;
}

 

    (4)通过 reset 方法初始化

#include <iostream>
#include <string>
#include <memory>
using namespace std;

int main()
{
    // 使用智能指针管理一块 int 型的堆内存, 内部引用计数为 1
    shared_ptr<int> ptr1 = make_shared<int>(520);
    shared_ptr<int> ptr2 = ptr1;
    shared_ptr<int> ptr3 = ptr1;
    shared_ptr<int> ptr4 = ptr1;
    cout << "ptr1管理的内存引用计数: " << ptr1.use_count() << endl;
    cout << "ptr2管理的内存引用计数: " << ptr2.use_count() << endl;
    cout << "ptr3管理的内存引用计数: " << ptr3.use_count() << endl;
    cout << "ptr4管理的内存引用计数: " << ptr4.use_count() << endl;

    ptr4.reset();
    cout << "ptr1管理的内存引用计数: " << ptr1.use_count() << endl;
    cout << "ptr2管理的内存引用计数: " << ptr2.use_count() << endl;
    cout << "ptr3管理的内存引用计数: " << ptr3.use_count() << endl;
    cout << "ptr4管理的内存引用计数: " << ptr4.use_count() << endl;

    shared_ptr<int> ptr5;
    ptr5.reset(new int(250));
    cout << "ptr5管理的内存引用计数: " << ptr5.use_count() << endl;

    return 0;
}

 

posted @ 2021-08-14 13:01  zwj鹿港小镇  阅读(85)  评论(0编辑  收藏  举报