I was wondering if the following is correct usage of a std::shared_ptr. All I want to do with the originally created pointer is add it onto the vector on class A, which will be later on retrieved
class A { public: void AddToVec(std::shared_ptr<B> b) { myVec_.push_back(b); } std::vector<std::shared_ptr<B>> GetVec() { return myVec_; } private: std::vector<std::shared_ptr<B>> myVec_; } Then on main, a pointer is created and passed with the following way
int main() { A a; std::shared_ptr<B> ptr = std::make_shared<B>(); a.AddToVec(std::move(ptr)); } - Is the usage of the
std::movecorrect on the main function? - Is it okay to simply create the
std::shared_ptronmainand move ownership using theAddToVecfunction?
std::move()isn't necessary.moveand when we don't.void AddToVec(std::shared_ptr<B>&& b)?