std::shared_ptr::reset
From cppreference.com
< cpp | memory | shared ptr
void reset();
|
(1) | (since C++11) |
template< class Y >
void reset( Y* ptr ); |
(2) | (since C++11) |
template< class Y, class Deleter >
void reset( Y* ptr, Deleter d ); |
(3) | (since C++11) |
template< class Y, class Deleter, class Alloc >
void reset( Y* ptr, Deleter d, Alloc alloc ); |
(4) | (since C++11) |
Replaces the managed object with an object pointed to by ptr
. Optional deleter d
can be supplied, which is later used to destroy the new object when no shared_ptr
objects own it. By default, delete expression is used as deleter. Proper delete expression corresponding to the supplied type is always selected, this is the reason why the function is implemented as template using a separate parameter Y
.
If *this
already owns an object and it is the last shared_ptr
owning it, the object is destroyed through the owned deleter.
If the object pointed to by ptr
is already owned, the function results in undefined behavior.
1) Releases the ownership of the managed object, if any. After the call, *this manages no object. Equivalent to shared_ptr().swap(*this);
2-4) Replaces the managed object with an object pointed to by
ptr
. Y
must be a complete type and implicitly convertible to T
. Additionally:
2) Uses the delete expression as the deleter. A valid delete expression must be available, i.e. delete ptr must be well formed, have well-defined behavior and not throw any exceptions. Effectively calls shared_ptr<T>(ptr).swap(*this);.
3) Uses the specified deleter
d
as the deleter. Deleter
must be callable for the type T
, i.e. d(ptr) must be well formed, have well-defined behavior and not throw any exceptions. Deleter
must be CopyConstructible
. Effectively calls shared_ptr<T>(ptr, d).swap(*this);.
4) Same as (3), but additionally uses a copy of
alloc
for allocation of data for internal use. Alloc
must be a Allocator
. The copy constructor and destructor must not throw exceptions. Effectively calls shared_ptr<T>(ptr, d, alloc).swap(*this);.
Contents |
[edit] Parameters
ptr | - | pointer to an object to acquire ownership of |
d | - | deleter to store for deletion of the object |
alloc | - | allocator to use for internal allocations |
[edit] Return value
(none)
[edit] Exceptions
1)
noexcept specification:
noexcept
2-4) As specified by the relevant constructor
.
This section is incomplete |
[edit] Example
Run this code
#include <memory> #include <iostream> struct Foo { Foo(int n = 0) noexcept : bar(n) { std::cout << "Foo: constructor, bar = " << bar << '\n'; } ~Foo() { std::cout << "Foo: destructor, bar = " << bar << '\n'; } int getBar() const noexcept { return bar; } private: int bar; }; int main() { std::shared_ptr<Foo> sptr = std::make_shared<Foo>(1); std::cout << "The first Foo's bar is " << sptr->getBar() << "\n"; // reset the shared_ptr, hand it a fresh instance of Foo // (the old instance will be destroyed ater this call) sptr.reset(new Foo); std::cout << "The second Foo's bar is " << sptr->getBar() << "\n"; }
Output:
Foo: constructor, bar = 1 The first Foo's bar is 1 Foo: constructor, bar = 0 Foo: destructor, bar = 1 The second Foo's bar is 0 Foo: destructor, bar = 0
[edit] See also
constructs new shared_ptr (public member function) |