std::shared_ptr初始化的几种方式

1.1 构造函数初始化

std::shared_ptr<int> pointer(new int(1));
 
std::shared_ptr<int> pointer1 = pointer;
 
std::shared_ptr<std::string> ss(new std::string("AAA"));
 
std::shared_ptr<std::string> = std::shared_ptr<std::string>(new std::string("AAA"));

1.2 std::make_shared初始化

std::shared_ptr<string> p3 = std::make_shared<string>();
 
std::shared_ptr<string> p2 = std::make_shared<string>("hello");
 
//auto关键字代替std::shared_ptr,p5指向一个动态分配的空vector<string>
auto p5 = make_shared<vector<string>>();

1.2 reset初始化

std::shared_ptr<int> pointer = nullptr;
pointer.reset(new int(1));

转载自:C++11 - std::shared_ptr初始化的几种方式 - StubbornHuang Blog

https://blog.csdn.net/qq_45052067/article/details/121606878

posted @ 2022-04-12 15:00  jiftle  阅读(938)  评论(0编辑  收藏  举报