日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

把类成员改成指针_如果类中存在管理其他类对象的指针,通过析构函数释放它们...

發布時間:2025/3/11 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 把类成员改成指针_如果类中存在管理其他类对象的指针,通过析构函数释放它们... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C.33: If a class has an owning pointer member, define a destructor

C.33:如果類包含擁有所有權的指針成員,定義析構函數Reason(原因)
An owned object must be deleted upon destruction of the object that owns it.從屬對象必須通過擁有該對象的所有者類的析構函數銷毀。

Example(示例)
A pointer member may represent a resource.A T* should not do so, but in older code, that's common. Consider a T* a possible owner and therefore suspect.指針成員可能用于表達某個資源。T*不應該這么做,但是在舊一些的代碼中,這種做法很常見。考慮到T*作為所有者使用的可能性,并確認。

templateclass Smart_ptr { T* p; // BAD: vague about ownership of *p // ...public: // ... no user-defined default operations ...};void use(Smart_ptr p1){ // error: p2.p leaked (if not nullptr and not owned by some other code) auto p2 = p1;}

Note that if you define a destructor, you must define or delete all default operations:注意:一旦定義了析構函數,就必須定義或者禁止所有的默認操作。

譯者注:這里的默認操作指的是默認構造函數,拷貝/移動構造函數,拷貝/移動運算符和析構函數。

templateclass Smart_ptr2 { T* p; // BAD: vague about ownership of *p // ...public: // ... no user-defined copy operations ... ~Smart_ptr2() { delete p; } // p is an owner!};void use(Smart_ptr2 p1){ auto p2 = p1; // error: double deletion}

The default copy operation will just copy the p1.p into p2.p leading to a double destruction of p1.p. Be explicit about ownership:默認拷貝操作只是將p1.p的值賦給p2.p(不包含其指向對象的拷貝),這會導致p1.p的雙重析構。明確所有權:

templateclass Smart_ptr3 { owner p; // OK: explicit about ownership of *p // ...public: // ... // ... copy and move operations ... ~Smart_ptr3() { delete p; }};void use(Smart_ptr3 p1){ auto p2 = p1; // OK: no double deletion}

譯者注:實際上并不是改變p的類型為owner就可以解決問題的。注意這段代碼通過注釋實現了拷貝和移動操作,而前一段代碼沒有。

Note(注意)

Often the simplest way to get a destructor is to replace the pointer with a smart pointer (e.g., std::unique_ptr) and let the compiler arrange for proper destruction to be done implicitly.一般來說,得到析構函數最簡單的方式是將指針換成智能指針(例如std::unique_ptr)并且讓編譯器提供適當的隱式執行的析構動作。

Note(注意)

Why not just require all owning pointers to be "smart pointers"? That would sometimes require non-trivial code changes and may affect ABIs.為什么不簡單地要求所有的所有者指針都變成“智能指針”?因為那樣做有時會引起重大的代碼變更并且影響二進制接口。

Enforcement(實施建議)

  • A class with a pointer data member is suspect.帶有指針類型數據成員的類都是可疑的。
  • A class with an owner should define its default operations.擁有owner成員的類應該定義默認操作。

譯者注:owner的定義就是T,只是在源代碼層次上增加了信息量,方便讀者理解和工具檢查。編譯器看起來和之前的T沒有任何區別,因此在二進制生成物層面上沒有變化,這樣就保證了既有代碼可以安全地引入這種做法。

原文鏈接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c33-if-a-class-has-an-owning-pointer-member-define-a-destructor


覺得本文有幫助?請分享給更多人。

更多文章請關注微信公眾號【面向對象思考】!

面向對象開發,面向對象思考!

總結

以上是生活随笔為你收集整理的把类成员改成指针_如果类中存在管理其他类对象的指针,通过析构函数释放它们...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。