C++ 项目中的 shared_ptr

shared_ptr 允许多个指针指向同一个对象;

shared_ptr < string > p1; // shared_ptr,可以指向string

shared_ptr < list<int> > p2; // shared_ptr, 可以指向int的list

注意智能指针是指针!指针所指向的对象有一个引用次数的属性,当引用次数为0时,该对象就会被析构。

class XnetNode

{

public:

int Init(void);

std::shared_ptr<xag_nav::os::StorageBase> mp_dcStorage;

}

int XnetNode::Init(void)

{

/* 初始化数据中心 */

while (nullptr == (mp_dcStorage = Application::Instance()->createStorage()))

{

XAG_LOG_E("failed to create storage");

std::this_thread::sleep_for(1s);

}

s32Ret = mp_dcStorage->setUserData(pType, const_cast<char *>(data.c_str()), data.size());

}

相关推荐
搬砖魁首28 分钟前
基础能力系列 - 多线程2 - 条件变量
c++·rust·条件变量·原子类型·线程同步互斥
chase_my_dream36 分钟前
C++ + SLAM 高频面试问题整理
开发语言·c++·面试
牛油果子哥q1 小时前
【C++ STL string 】C++ STL string 终极精讲:底层原理、内存机制、全套API、深浅拷贝、易错坑点与工程实战规范
数据库·c++
凡人叶枫3 小时前
Effective C++ 条款04:确定对象被使用前已先被初始化
java·linux·开发语言·c++·嵌入式开发
不想写代码的星星3 小时前
std::move 根本不移动,就像老婆饼里没有老婆
c++
redaijufeng3 小时前
C++雾中风景7:闭包
c++·算法·风景
小欣加油4 小时前
leetcode287寻找重复数
数据结构·c++·算法·leetcode
思麟呀4 小时前
C++11 核心特性(三):强类型枚举、static_assert 与 std::tuple
开发语言·c++
一拳一个呆瓜4 小时前
【STL】C++程序的启动与终止
c++·stl
凡人叶枫4 小时前
Effective C++ 条款07:为多态基类声明 virtual 析构函数
linux·c语言·开发语言·c++