I have a very basic question about std::shared_ptr. If I have some
std::shared_ptr<A> p;
, where A is some class defined in my code, what is the preferred way to pass this std::shared_ptr as a function argument? Does it make a sense to define my functions that does something with the instance of class A like this:
void func(A &a);
and pass *p to these functions, and then convert 'a' back to std::shared_ptr with shared_from_this() if needed? Is there a reason to do so and what is the common practice? Or I should pass shared_ptr as the argument like this:
void func(std::shared_ptr<A> p); //thread safe
void func(const std::shared_ptr<A> & p); //not thread safe
In the most functions in my project I can use both alternatives, but my colleague said that the first alternative is better because the reference (A&) is always better than a pointer (shared_ptr or A*), because the reference semantics implies that the argument is not null.
Aucun commentaire:
Enregistrer un commentaire