c++ - Will unique_ptr take control of any pointer it is passed, regardless of how it was created? -
i have been doing std::vector
of unique_ptr
s:
m_stuff.push_back(std::unique_ptr<thing> (new derivedthing()));
this gives me automatic memory deletion when elements in vector removed.
i have scenario whereby else creates heap memory via new
, generic pointer exists. now, suppose this:
m_stuff.push_back(std::unique_ptr<thing> (thingy)); //thingy thing*
can safely assume this, vector m_stuff
has control on thingy
such when removed vector, delete
d? of course, there possibility of dangling pointer if thingy
copied or stored elsewhere different issue.
unique_ptr
has nothing magical in it. has jus regular constructor (actually has few) takes pointer of type t
, becomes responsible deleting pointee. yes, not care (and can't care) did pointer came from
Comments
Post a Comment