std::shared_ptr简单用法demo

std::shared_ptr简单用法

定义于头文件 <memory>

增加计数

被引用则会增加计数

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

int main()
{
    std::shared_ptr<string> sp = std::make_shared<string>(10, 'a');
    std::shared_ptr<string> sp1(sp);//再次被引用则计数+1
    cout << sp1.use_count() << endl;//2
}

在函数内改变计数,超过生命周期后计数会恢复,test函数内的sp2析构了。

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

void test(shared_ptr<int>& sp)
{
    std::shared_ptr<int> sp2(sp);
    int n = sp2.use_count();
    cout << n << endl;
}

int main()
{
    int* p1 = new int[3];
    memset(p1, 0, sizeof(int) * 3);
    std::shared_ptr<int> sp1(p1);
    test(sp1);
    cout << sp1.use_count() << endl;
}

输出结果是

2
1

得到原指针

get()函数返回原指针

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

int main()
{
    int* p1 = new int[3];
    memset(p1, 0, sizeof(int) * 3);
    *p1 = 1;
    std::shared_ptr<int> sp1(p1);
    cout << *(sp1.get()) << endl;
}

输出为 1

创建shared_ptr实例

最安全和高效的方法是调用make_shared库函数,该函数会在堆中分配一个对象并初始化,最后返回指向此对象的share_ptr实例。如果你不想使用make_shared,也可以先明确new出一个对象,然后把其原始指针传递给share_ptr的构造函数。make_shared相当于是一个包装函数。

示例如下:

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

int main()
{
    // 传递给make_shared函数的参数必须和shared_ptr所指向类型的某个构造函数相匹配
    shared_ptr<string> pStr = make_shared<string>(10, 'a');
    cout << *pStr << endl; //  aaaaaaaaaa

    int* p = new int(5);
    shared_ptr<int> pInt(p);
    cout << *pInt << endl; // 5
}

声明方法

//way1
auto sp1 = std::make_shared<int>(5);

//way2
int* p1 = new int[3];
memset(p1, 0, sizeof(int) * 3);
std::shared_ptr<int> sp2(p1);

访问所指对象

shared_ptr的使用方式与普通指针的使用方式类似,既可以使用解引用操作符*获得原始对象进而访问其各个成员,也可以使用指针访问符->来访问原始对象的各个成员。

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

class Example
{
public:
    Example() : e(1) { cout << "Example Constructor..." << endl; }
    ~Example() { cout << "Example Destructor..." << endl; }

    int e;
};

int main() {
    shared_ptr<Example> pInt(new Example());
    cout << pInt->e << endl;
}

输出结果:

Example Constructor...
1
Example Destructor...

拷贝和赋值操作

我们可以用一个shared_ptr对象来初始化另一个share_ptr实例,该操作会增加其引用计数值。

例如:

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

int main()
{
    shared_ptr<string> pStr = make_shared<string>(10, 'a');
    cout << pStr.use_count() << endl; //  1
    shared_ptr<string> pStr2(pStr);
    cout << pStr.use_count() << endl;  //  2
    cout << pStr2.use_count() << endl; //  2
}

检查引用计数

shared_ptr提供了两个函数来检查其共享的引用计数值,分别是unique()和use_count()。

在前面,我们已经多次使用过use_count()函数,该函数返回当前指针的引用计数值。值得注意的是use_count()函数可能效率很低,应该只把它用于测试或调试。

unique()函数用来测试该shared_ptr是否是原始指针唯一拥有者,也就是use_count()的返回值为1时返回true,否则返回false。

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

int main()
{
    shared_ptr<string> pStr = make_shared<string>(10, 'a');
    cout << pStr.unique() << endl; //  true
    shared_ptr<string> pStr2(pStr);
    cout << pStr.unique() << endl;  //  false
    cout << pStr2.unique() << endl; //  false
}

观察释放资源

当引用计数为0时,就会释放资源。

#include <iostream>
#include <memory>
using namespace std;
class Example {
public:
    Example(string n)
        : name(n)
    {
        cout << n << " constructor..." << endl;
    }
    ~Example() { cout << name << " destructor..." << endl; }

    string name;
};

int main()
{

    shared_ptr<Example> pStr = make_shared<Example>("a object");
    shared_ptr<Example> pStr2 = make_shared<Example>("b object");
    cout << pStr.use_count() << endl;
    cout << pStr2.use_count() << endl;

    pStr = pStr2; // 此后pStr和pStr2指向相同对象
    cout << pStr->name << endl;
    cout << pStr2->name << endl;
}

输出结果

a object constructor...
b object constructor...
1
1
a object destructor...
b object
b object
b object destructor...
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值