This pattern is licensed under the CC0 Public Domain Dedication.
Requires c++11 or newer.
Share ownership of a dynamically allocated object with another unit of code.
On line 11, we create a
which has ownership of a dynamically allocated
(allocated with the
utility function). Line 12 then demonstrates sharing ownership of this
object with a function. That is, both
func have access
to the same
foo object. When ownership of an object is shared, it
will only be destroyed when all
std::shared_ptrs owning it are
In other cases, you may instead wish to transfer unique ownership of an object.
27 August 2018