c++ - Passing a unique_ptr to a function which adds it to a vector -
I have a class that holds a vector of unique_ptr. There is also a way to add an element to the vector in the square.
class PointerVector {public: zero addPointer (std :: unique_ptr gt; p); Private: std :: vector & lt; Std :: unique_ptr & lt; Int & gt; & Gt; MPointers; }; addPointer method definition: zero PointerVector :: addPointer (std :: unique_ptr gt; p ) {MPointers .emplace_back (std :: move (p)); } So I use std :: move to move the owner of the indicator. When I call addPointer , I also use std :: move : PointerVector pv; Std :: unique_ptr & lt; Integer & gt; I (new I (1)); Pv.addPointer (std :: move (i)); Is this the right way to do the job? std :: move feels like the hassle of calling all the time. Edit : I was really a unique / square, but I wanted to try things first with an int to make things easier.
Almost you do not have to use std :: move () After the temporary passage, your final piece will look better as below (I'm not asking why you need to store a single intestine with a smart pointer from an int). PointerVector pv; Pv.addPointer (std :: unique_ptr & lt; int & gt; (new int (1)));
Comments
Post a Comment