shared_ptr智能指针
2017-05-12 22:36
344 查看
#include <iostream> #include <memory> #include <string> /*shared_ptr类 智能指针也是模板 */ void f(std::shared_ptr<std::string> p) { std::string* plocal = p.get();//智能指针的get()函数, std::cout << *plocal << std::endl;//打印普通指针 所指的内容 return; } int main(int argc, char const *argv[]) { std::shared_ptr<std::string> p1(new std::string ("hello world"));//显式 f(p1); std::shared_ptr<int> p2 = std::make_shared<int>(52);//最安全的分配和使用 std::shared_ptr<std::string> p3 = std::make_shared<std::string>(10,'a'); std::cout << "p2: " << *p2 << "p3 : " << *p3 << std::endl; auto a = std::make_shared<int>(42); std::cout << *a << std::endl; return 0; }
编译时,记得加上 -std=c++11选项!
-----------------
知识无价!
相关文章推荐
- c++:分析智能指针shared_ptr存在的循环引用的缺陷
- 智能指针的模拟实现 auto_ptr scoped_ptr shared_ptr
- 【C++】智能指针shared_ptr 定位删除器(仿函数)
- C++11智能指针之使用shared_ptr实现多态
- boost中的智能指针shared_ptr的指针管理
- 【C++标准模板库笔记2】智能指针shared_ptr的简单使用
- 智能指针(auto_ptr,scoped_ptr,shared_ptr)
- 智能指针之 shared_ptr
- c++智能指针:auto_ptr shared_ptr
- c++11:智能指针之shared_ptr
- 智能指针3---SharedPtr
- C++简单版shared_ptr智能指针的实现
- shared_ptr 共享指针 智能指针 简介
- 有关智能指针(shared_ptr)的讨论
- 智能指针(二):shared_ptr实现原理
- shared_ptr智能指针源码剖析
- 智能指针shared_ptr使用学习
- C++智能指针shared_ptr分析
- boost::shared_ptr智能指针分析
- 智能指针tr1::shared_ptr、boost::shared_ptr使用