C++ - std::shared_ptr::reset

5460 ワード

C++ - std::shared_ptr::reset
public member function-公開メンバー関数Defined in header -ヘッダファイルに定義
1. std::shared_ptr::reset
(1)	void reset() noexcept;
(2)	template  void reset (U* p);
(3)	template  void reset (U* p, D del);
(4)	template  void reset (U* p, D del, Alloc alloc);

管理するオブジェクトを置き換えます.
Reset pointerリセットポインタ.
For signature (1) the object becomes empty (as if default-constructed ). signature(1)の場合、オブジェクトはempty(as if default-constructed)になります.
In all other cases, the shared_ptr acquires ownership of p with a use count of 1, and -optionally- with del and/or alloc as deleter and allocator , respectively. 他のすべての場合において、shared_ptrは、使用カウント1のpの所有権を獲得し、del and/or alloc as deleter and allocator、respectivelyを使用することができる(オプション).
Additionally, a call to this function has the same side effects as if shared_ptr 's destructor was called before its value changed (including the deletion of the managed object if this shared_ptr was unique). また、関数の呼び出しには、値を変更する前にshared_ptrの構造関数が呼び出されたように、同じ副作用があります(このshared_ptrが一意であれば、管理オブジェクトの削除も含まれます).
2. Parameters p Pointer whose ownership is taken over by the object. 所有権がオブジェクトによって引き継がれるポインタ.
Generally, this pointer should not be already managed by any other managed pointer (i.e., this value should not come from calling member get on a managed pointer). 通常、ポインタは、他のmanaged pointerによって管理されてはならない(すなわち、この値は、管理を呼び出すポインタ上のメンバーgetから来てはならない).U* shall be implicitly convertible to T* (where T is shared_ptr 's template parameter). U*は、T*に暗黙的に変換されるべきである(ここで、Tshared_ptrのテンプレートパラメータである).
所有権を取得するオブジェクトへのポインタ.del Deleter object to be used to release the owned object. Deleterオブジェクト.所有するオブジェクトを解放します.
This shall be a callable object taking a pointer to T as argument for its functional call (where T is shared_ptr 's template parameter). これは、Tを指すポインタをその関数として呼び出すパラメータ(Tshared_ptrのテンプレートパラメータ)として呼び出すことができるオブジェクトになります.
オブジェクトを削除するために格納された削除器.alloc Allocator object used to allocate/deallocate internal storage. 内部ストレージの割り当て/割り当て解除に使用されるディスペンサオブジェクト.
内部ストレージに使用するディスペンサ.
3. Return value
none
4. Examples
4.1 std::shared_ptr::reset
//============================================================================
// Name        : std::shared_ptr::reset
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include 
#include 

int main()
{
	std::shared_ptr sp; // empty

	sp.reset(new int); // takes ownership of pointer
	*sp = 10;
	std::cout << *sp << '
'; sp.reset(new int); // deletes managed object, acquires new pointer *sp = 20; std::cout << *sp << '
'; sp.reset(); // deletes managed object return 0; }
10
20
       . . .
assignment [ə'saɪnmənt]:n.   ,  ,  

4.2 std::shared_ptr::reset
//============================================================================
// Name        : std::shared_ptr::reset
// Author      : Yongqiang Cheng
// Version     : Version 1.0.0
// Copyright   : Copyright (c) 2019 Yongqiang Cheng
// Description : Hello World in C++, Ansi-style
//============================================================================

#include 
#include 

struct Foo {
	Foo(int n = 0) noexcept : bar(n)
	{
		std::cout << "Foo: constructor, bar = " << bar << '
'; } ~Foo() { std::cout << "Foo: destructor, bar = " << bar << '
'; } int getBar() const noexcept { return bar; } private: int bar; }; int main() { std::shared_ptr sptr = std::make_shared(1); std::cout << "The first Foo's bar is " << sptr->getBar() << "
"; // 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() << "
"; return 0; }
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
       . . .

References
http://www.cplusplus.com/reference/memory/shared_ptr/reset/https://en.cppreference.com/w/cpp/memory/shared_ptr/reset