如何使用share_ptr智能指针

2025-04-21 06:58:01

1、定义std::shared_ptr变量的时候,同时初始化内容

如何使用share_ptr智能指针

2、通过std::shared_ptr的函数get取得原始对象的指针,然后输出信息

如何使用share_ptr智能指针

3、创建std::shared_ptr变量的时候,使用另一个std::shared_ptr类型来初始化

如何使用share_ptr智能指针

4、从输出结果看,新的对象std::shared_ptr输出的内容与复制过来的对象的内容一致

如何使用share_ptr智能指针

5、采用函数make_share来分配内存初始化std::shared_ptr变量

如何使用share_ptr智能指针

6、通过直接取std::shared_ptr变量的值,可以输出std::shared_ptr变量存储的内容

如何使用share_ptr智能指针

7、std::shared_ptr类型也可以存储数组,但是需要添加对应的删除器

如何使用share_ptr智能指针

8、通过采用函数strcpy对std::shared_ptr中存放的数组进行赋值,能够输出正确的信息,具体输出结果如下图红色箭头所指示的位置。

如何使用share_ptr智能指针
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢