智能指针使用指南

来源:互联网 发布:有哪些动画培训班知乎 编辑:程序博客网 时间:2024/06/04 19:09

智能指针使用指南

### SmartPtr Guideline

void f( widget* );              (a)void f( widget& );              (b)void f( unique_ptr<widget> );   (c)void f( unique_ptr<widget>& );  (d)void f( shared_ptr<widget> );   (e)void f( shared_ptr<widget>& );  (f)

Guideline #1:
Don’t pass a smart pointer as a function parameter unless you want to use or manipulate the smart pointer itself, such as to share or transfer ownership.

Guideline #2:
Prefer passing objects by value, *, or &, not by smart pointer.

Guidline #3:
As usual, use a * if you need to express null (no widget), otherwise prefer to use a &; and if the object is input-only, write const widget* or const widget&.

Guideline #4:
Express a “sink” function using a by-value unique_ptr parameter.

ref:
https://herbsutter.com/2013/06/05/gotw-91-solution-smart-pointer-parameters/amp/

0 0