Don't use get to initialize or assign another smart pointer.

The code that use the return from get can not delete the pointer

Although the compiler will not complain, it is an error to build another smart pointer to the pointer returned by get

shared_ptr<int> p(new int());    // reference count is 1
int *q = p.get(); // ok; but do not delete its pointer
{
shared_ptr<int> (q);
} // block ends. q is destroyed, and the memory to which q points is freed
int f = *p; // undefined. The memory to which p points is freed.

Using Our Own Deletion Code

void end_connection(connection *p)
{
disconnection(*p);
} void f(destination &d)
{
connection c = conn(&d);
shared_ptr<connection p(&c, end_connection);
// use the connection
// when f exists, even if by an exception, the connection resource will be properly closed
}

For unique_ptr

Call release() breaks the connection between a unique_ptr and the object it had been managing. Ofter the pointer returned by release() is used to initialized or assign another pointer

unique_ptr<int> p2(new int());
p2.release(); // WRONG! P2 will not free the memory, and we have lose the pointer
auto p = p2.release(); // OK, but we must remember to delete p

Backward compatibilities auto_ptr

Although auto_ptr is still part of the standard library, programs should use unique_ptr instead.

最新文章

  1. Linux命令学习总结:rm命令
  2. SQL Server 2014新特性——事务持久性控制
  3. 可复用View的PagerAdapter
  4. Oracle的表空间和数据文件
  5. wordpress的创建
  6. sqlsever备份,还原和导入导出方法
  7. CFBundleVersion与CFBundleShortVersionString
  8. ArcGIS Engine 图层裁剪 Clip的实现方法
  9. hdu3270Arranging Your Team(dfs)
  10. 用HTML5 Canvas为网页添加动态波浪背景
  11. UVA 10917 Walk Through the Forest(dijkstra+DAG上的dp)
  12. Android Service with Delphi 10 Seattle
  13. HDU -2674 N!Again(小技巧)
  14. pyqt lineedit右边显示按钮效果
  15. java界面编程(8) ------ 组合框(下拉列表)
  16. vs2012常用快捷键总结
  17. Spring-boot:5分钟整合Dubbo构建分布式服务
  18. 爱奇艺直播 - 春晚直播业务API架构
  19. Finance财务软件(引入业务系统凭证专题)
  20. 前端基础之初识HTML

热门文章

  1. iOS 中系统与 SDK 版本检测
  2. Caused by: java.lang.ClassNotFoundException: org.springframework.boot.bind.RelaxedPropertyResolver
  3. Vue聊天框默认滚动到底部
  4. MySQL学习之路(一)——初涉MySQL。
  5. 关于python文件问题
  6. Msql浅析-基础命令(二)
  7. nexus3使用docker运行/创建docker私有仓库/maven私有仓库
  8. GC错误
  9. #《JAVA程序设计》 20155214 实验五 网络编程与安全
  10. 20155311高梓云补交的Mypc课下实践