Agr*_*hak 2 c++ pointers function unique-ptr raw-pointer
您的经验中更常见的是:func1()或funct2()?假设func1和func2最好不要作为Foo类方法.
void func1(unique_ptr<Bar>& bar) { /* alter pointed to object's attributes */ }
void func2(Bar* const bar) { /* alter pointed to object's attributes */ }
class Foo
{
unique_ptr<Bar> bar;
void mutate_bar1(){ func1(bar); }
void mutate_bar2(){ func2(bar.get()); }
}
Run Code Online (Sandbox Code Playgroud)
准则:除非您想使用或操纵智能指针本身,例如共享或转移所有权,否则不要将智能指针作为函数参数传递.
指南:首选按值传递对象
*,或者&不是通过智能指针传递对象.
像往常一样,
*如果你需要表达null(没有widget),否则更喜欢使用a&; 如果对象是仅输入,则写const widget*或const widget&.
| 归档时间: |
|
| 查看次数: |
657 次 |
| 最近记录: |