void reset() noexcept; | (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.
*this
manages no object. Equivalent to shared_ptr().swap(*this);
ptr
. Y
must be a complete type and implicitly convertible to T
. Additionally:delete ptr
must be well formed, have well-defined behavior and not throw any exceptions. Equivalent to shared_ptr<T>(ptr).swap(*this);
.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, and its copy constructor and destructor must not throw exceptions. Equivalent to shared_ptr<T>(ptr, d).swap(*this);
.alloc
for allocation of data for internal use. Alloc
must be a Allocator. The copy constructor and destructor must not throw exceptions. Equivalent to shared_ptr<T>(ptr, d, alloc).swap(*this);
.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 |
(none).
std::bad_alloc
if required additional memory could not be obtained. May throw implementation-defined exception for other errors. delete ptr
is called if an exception occurs.std::bad_alloc
if required additional memory could not be obtained. May throw implementation-defined exception for other errors. d(ptr)
is called if an exception occurs.#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 after 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
constructs new shared_ptr (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/memory/shared_ptr/reset