c++ - 此 shared_ptr 示例中的内容不清楚

标签 c++ boost

#include <iostream>
#include <boost/shared_ptr.hpp>

class implementation
{
public:
    ~implementation() { std::cout <<"destroying implementation\n"; }
    void do_something() { std::cout << "did something\n"; }
};

void test()
{
    boost::shared_ptr<implementation> sp1(new implementation());
    std::cout<<"The Sample now has "<<sp1.use_count()<<" references\n";

    boost::shared_ptr<implementation> sp2 = sp1;
    std::cout<<"The Sample now has "<<sp2.use_count()<<" references\n";

    sp1.reset();
    std::cout<<"After Reset sp1. The Sample now has "<<sp2.use_count()<<" references\n";

    sp2.reset();
    std::cout<<"After Reset sp2.\n";
}

int main()
{
    test();
}

运行结果如下:

$ ./a.out 
The Sample now has 1 references
The Sample now has 2 references
After Reset sp1. The Sample now has 1 references
destroying implementation
After Reset sp2.

请检查上面的代码。我不清楚的第一件事是下面这句话是什么意思?所以 sp1 是一个指针?功能?还是指向函数的指针? new implementation() 意味着什么? sp1()的参数?

boost::shared_ptr<implementation> sp1(new implementation());

第二个问题是 destroying implementationsp1.reset()sp2.reset() 的结果。但是如果sp1.reset()被注释掉,那么结果会是:

$ ./a.out 
The Sample now has 1 references
The Sample now has 2 references
After Reset sp1. The Sample now has 2 references
After Reset sp2.
destroying implementation

如果我们只注释掉sp2.reset(),那么结果会是:

$ ./a.out 
The Sample now has 1 references
The Sample now has 2 references
After Reset sp1. The Sample now has 1 references
After Reset sp2.
destroying implementation

所以没有必要同时调用 sp1.reset()sp2.reset() 来释放 shared_ptr,对吗?

最佳答案

The first thing unclear to me is that what does below sentence mean? So sp1 is a pointer? a function? or a pointer to a function?

spshared_ptr<implementation> .如果您不知道这意味着什么,可以查看引用文档和教程。但简短的版本是:它是一个类似于 implementation * 的对象。指针,除了它会自动删除 implementation对象当你完成它。这就是使它成为“智能指针”的原因。 shared_ptr是一种特殊类型的智能指针,可以让您复制任意数量的拷贝,并且仅在所有这些拷贝都消失时才删除底层对象。

一种理解方式是,它为您提供了一种不需要垃圾收集器的简单垃圾收集形式。

另一种看待它的方式是作为 Resource Acquisition Is Initialization 的一部分(RAII),C++ 的核心习语之一。

and new implementation() means what? The argument of sp1()?

new implementation()创建一个新的 implementation对象,调用其默认构造函数,并返回 implementation *指针。该指针是 shared_ptr 的参数构造函数。这意味着 sp1成为指向新 implementation 的智能指针对象,以便在 sp1 时该对象将被销毁和删除,以及后来制作的任何拷贝,都将消失。

The second question is that destroying implementation is given as a result of sp1.reset() and sp2.reset().

实际上,它是sp1 的结果和 sp2指向新值被破坏。 reset做前者,但只是什么也不做,让他们离开范围做后者。这是 RAII 的主要内容。

So it is not necessary to call both sp1.reset() and sp2.reset() to release the shared_ptr, am I right?

没错。您很少想显式调用 reset . RAII 的全部意义在于您不必手动管理这些事情;您初始化一个对象(如 shared_ptr )以获取对资源的访问权限,然后让该对象消失以释放访问权限。

在某些情况下它很有用。例如,如果您有一个 shared_ptr作为对象的成员,并且该对象的持续时间比它拥有的资源长得多,您可以通过调用 reset 提前释放它. (如果您同时将拷贝传递给其他人,则不必担心它会被提前删除——这只是意味着您不再参与保存它。)

关于c++ - 此 shared_ptr 示例中的内容不清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14108794/

相关文章:

c++ - 一般指针问题

c++ - 将范围有限的 float 转换为保持排序顺序和精度的无符号整数?

c++ - 将(隐式)boost shared_ptr<T> 转换为 shared_ptr<const T>

c++ - 我无法理解为什么从子类中删除变量后输出会发生变化

c++ - C++ lambda 函数的默认调用约定是什么?

c++ - 使用提升多精度的数学精度问题

c++ - 如何使用 boost::log::BOOST_TRIVIAL_LOG 更改默认格式?

c++ - 如何使用 GIL 迭代器设置像素

c++ - 在编译时检测一个类是否有成员变量或函数

c++ - 如果满足特定条件,则停止沿特定深度的 boost::depth_first_search