从具备专属所有权的指针std::unique_ptr触发构造一个std::shared_ptr时,会创建一个控制块,而专属所有权的智能指针会被置空。 当std::shared_ptr构造函数使用裸指针作为实参来调用时,会创建一个控制块。 注:如果std::shared_ptr的构造函数传递std::shared_ptr或者std::weak_ptr作为实参,是不会创建新的控制块。
这里将该指针初始化为0代表将该指针变量置空,因为当前shared_ptr未指向任何对象,暂时不需要控制块数据...
weak_ptr 不进行计数,并且不能操作内存,当前赋值的 shared_ptr 销毁后,weak_ptr 也会置空 weak_ptr works together with shared_ptr. When using both, it's still true that the object the pointers point at normally gets destroyed as soon as and only when no shared_ptr points at it any more. ...
我们在对std::shared_ptr的引用计数进行操作时,其成本是很高的,因为这个是原子操作。所以当我们对一个std::shared_ptr进行移动构造时,实际上是非常迅速的,因为这个操作仅仅是将源std::shared_ptr置空,其引用计数不变。 与std::unique_ptr类似,std::shared_ptr也可以自定义析构器,且析构器的大小不会影响std::...
shared_ptr 还有个非常令人厌恶的特点,那就是传染性极强,只有在一处有了shared_ptr,所有出现这个对象...