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
struct A {
shared_ptr msg;
shared_ptr * ptr_msg;
}
pass by value:
void set(shared_ptr msg) {
this->msg = msg; /// create a new shared_ptr, reference count will be added;
} /// out of method, new created shared_ptr will be deleted, of course, reference count also be reduced;
pass by reference:
void set(shared_ptr& msg) {
this->msg = msg; /// reference count will be added, because reference is just an alias.
}
pass by pointer:
void set(shared_ptr* msg) {
this->ptr_msg = msg; /// reference count will not be added;
}