mardi 30 août 2022

Is it necessary to passing smart pointer object by reference?

assume I have a class, which contains a smart pointer as its member variable.

please see the code:

class B;

class A {
 public:
  A(const std::shared_ptr<B>& b) : b_(b) {}  // option1: passing by reference
  A(std::shared_ptr<B> b) : b_(b) {}  // option2: passing by value
  std::shared_ptr<B> b_;
};

I have two choice for A's constructor: construct by smart pointer and construct by smart pointer's reference.

could you help on this? what is the advantages and disadvantages of these two method?

will the copy between smart pointer cost big waste?

Aucun commentaire:

Enregistrer un commentaire