久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我什么時候應該使用原始指針而不是智能指針?

When should I use raw pointers over smart pointers?(我什么時候應該使用原始指針而不是智能指針?)
本文介紹了我什么時候應該使用原始指針而不是智能指針?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

閱讀后這個答案,看起來最好使用 智能指針 盡可能多,并將普通"/原始指針的使用減少到最低限度.

After reading this answer, it looks like it is a best practice to use smart pointers as much as possible, and to reduce the usage of "normal"/raw pointers to minimum.

這是真的嗎?

推薦答案

不,這不是真的.如果一個函數(shù)需要一個指針并且與所有權無關,那么我強烈認為應該傳遞一個常規(guī)指針,原因如下:

No, it's not true. If a function needs a pointer and has nothing to do with ownership, then I strongly believe that a regular pointer should be passed for the following reasons:

  • 沒有所有權,因此您不知道要傳遞什么樣的智能指針
  • 如果你傳遞一個特定的指針,比如shared_ptr,那么你將無法傳遞,比如,scoped_ptr
  • No ownership, therefore you don't know what kind of a smart pointer to pass
  • If you pass a specific pointer, like shared_ptr, then you won't be able to pass, say, scoped_ptr

規(guī)則是這樣的——如果你知道一個實體必須擁有對象的某種所有權,總是使用智能指針——它給你您需要的所有權類型.如果沒有所有權的概念,從不使用智能指針.

The rule would be this - if you know that an entity must take a certain kind of ownership of the object, always use smart pointers - the one that gives you the kind of ownership you need. If there is no notion of ownership, never use smart pointers.

示例 1:

void PrintObject(shared_ptr<const Object> po) //bad
{
    if(po)
      po->Print();
    else
      log_error();
}

void PrintObject(const Object* po) //good
{
    if(po)
      po->Print();
    else
      log_error();
}

示例 2:

Object* createObject() //bad
{
    return new Object;
}

some_smart_ptr<Object> createObject() //good
{
   return some_smart_ptr<Object>(new Object);
}

這篇關于我什么時候應該使用原始指針而不是智能指針?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區(qū)別?)
Difference between const. pointer and reference?(常量之間的區(qū)別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態(tài)?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 成人精品一区二区三区中文字幕 | 日韩综合在线 | 国产高清一二三区 | 国产97碰免费视频 | 五月天婷婷综合 | 高清黄色毛片 | 深爱激情综合 | 国产免费自拍 | 色一情一乱一伦一区二区三区 | 亚洲精品乱码久久久久久蜜桃91 | 日韩av一区二区在线 | 国产成人在线一区二区 | 亚洲一区二区日韩 | 日本高清视频在线播放 | 日韩成人一区二区 | 精品一二区 | 美国一级黄色片 | 亚洲国产精品成人无久久精品 | 91精品国产91久久久久久 | 国产免费观看久久黄av片涩av | 午夜精品一区二区三区在线观看 | 成人在线免费网站 | 精品国产91亚洲一区二区三区www | 精品在线观看入口 | 国产黄色在线观看 | 在线免费激情视频 | 久久99这里只有精品 | 欧美一级免费 | 美女视频一区二区三区 | 91一区 | 亚洲精选一区二区 | 欧美一区在线看 | 国产精品国产精品国产专区不卡 | heyzo在线| 欧美性大战xxxxx久久久 | 激情国产| hsck成人网 | 亚洲欧美激情视频 | www日本在线播放 | 国产亚洲精品久久久优势 | 亚洲一区二区在线播放 |