C++ - std::shared_ptr::reset

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 << '\n';

	sp.reset(new int); // deletes managed object, acquires new pointer
	*sp = 20;
	std::cout << *sp << '\n';

	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 << '\n';
	}

	~Foo()
	{
		std::cout << "Foo: destructor, bar = " << bar << '\n';
	}

	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() << "\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";

	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

你可能感兴趣的:(C++)