c++ - How can I create a shared_ptr to a std::vector? -
I need to create shared_ptr on a std :: vector, what is the correct syntax?
std :: vector & lt; Uint8_t & gt; MVector; Shared_ptr & lt; Std :: vector & lt; Uint8_t & gt; & Gt; MSharedPtr = & amp; MVector;
The above code does not compile.
Thank you.
Let's make a smart pointer manage the stack object that you are trying to do. This does not work, because the stack object is going to kill itself, when it comes out of the scope. Smart Pointer can not stop it from doing this.
std :: shared_ptr & lt; Std :: vector & lt; Uint8_t & gt; & Gt; SP; {Std :: vector & lt; Uint8_t & gt; MVector; SP = std :: shared_ptr & lt; Std :: vector & lt; Uint8_t & gt; & Gt; (& Amp; mVector); } Sp-> Empty (); // anchor reference, as the mVector has already been destroyed
three options:
shared_ptr
: automatically mSharedPtr = std :: make_shared & lt; Std :: vector & lt; Uint8_t & gt; Let's manage by & Gt; (/ * Vector constructor arguments * /);
(2) Manage a copy of the vector (by applying the vector copy constructor):
std :: Vector & LT; Uint8_t & gt; MVector; Auto mSharedPtr = std :: make_shared & lt; Std :: vector & lt; Uint8_t & gt; & Gt; (MVector);
(3) Move vector (by implementing vector driving creator):
std :: Vector & lt; Uint8_t & gt; MVector; Auto mSharedPtr = std :: make_shared & lt; Std :: vector & lt; Uint8_t & gt; & Gt; (Std :: move (mVector)); // Now do not use mVector
Comments
Post a Comment