C++ - passing references to std::shared_ptr or boost::shared_ptr

前端 未结 17 1666
日久生厌
日久生厌 2020-11-28 01:20

If I have a function that needs to work with a shared_ptr, wouldn\'t it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr

17条回答
  •  余生分开走
    2020-11-28 01:54

    In the second case, doing this is simpler:

    Class::only_work_with_sp(foo &sp)
    {    
        ...  
        sp.do_something();  
        ...  
    }
    

    You can call it as

    only_work_with_sp(*sp);
    

提交回复
热议问题