zoukankan      html  css  js  c++  java
  • 16.2.2 有关智能指针的注意事项

    应避免多个智能指针指向同一个堆内存时引发的多次删除同一个堆内存空间的情况。

    auto_ptr    // 使用赋值操作时将转让所有权(对堆内存的所有权)
    unique_ptr    // 与auto_ptr同,但更严格
    shared_ptr    // 跟踪引用特定对象的智能指针数,仅当最后一个指针过期时,才调用delete

    ### unique_ptr与auto_ptr的差别在与前者在编译阶段差错而auto_ptr在运行阶段才能检测到错误(前者更安全)

    ### 还需注意的是,程序试图将一个unique_ptr赋值给另一个时(shared_ptr和auto_ptr也行),如果源unique_ptr是一个临时右值(用作函数返回值),

    ### 编译器允许这样做,如果源 unique_ptr  将存在一段时间,编译器将禁止这样做

    ### 第三点是auto_ptr、shared_ptr只能使用new版本,而unique_ptr还支持new[ ]版本(使用delete[ ])

  • 相关阅读:
    Munge
    file upload custom form
    随笔摘要
    生成css 和 清缓存
    drupal commit 原则
    Git reset --hard
    www-data
    301/302的区别
    什么是request_uri
    in_array foreach array_search的性能比较
  • 原文地址:https://www.cnblogs.com/suui90/p/13545107.html
Copyright © 2011-2022 走看看