class A01 {
public:
//std::weak_ptr<B0> b_ptr;
int data{ 1234 };
~A01() {
std::cout << "A01 deleted\n";
}
void Print() { std::cout << "I'm A01\n"; }
};
auto main444() -> int
{
std::shared_ptr<A01> ptr = std::make_shared<A01>(); //ptr:1, ctrl_block:1
auto sec = ptr; //这一步调用的哪个函数
return 1;
}
int main()
{
main444();
return 1;
}
auto sec = ptr; //这一步调用的哪个函数?
答案是:
shared_ptr(const shared_ptr& _Other) noexcept
{ // construct shared_ptr object that owns same resource as _Other
this->_Copy_construct_from(_Other);
}
template<class _Ty2>
void _Copy_construct_from(const shared_ptr<_Ty2>& _Other)
{ // implement shared_ptr's (converting) copy ctor
if (_Other._Rep)
{
_Other._Rep->_Incref();
}
_Ptr = _Other._Ptr;
_Rep = _Other._Rep;
}
class A01 {
public:
//std::weak_ptr<B0> b_ptr;
int data{ 1234 };
~A01() {
std::cout << "A01 deleted\n";
}
void Print() { std::cout << "I'm A01\n"; }
};
auto main444() -> int
{
std::shared_ptr<A01> ptr = std::make_shared<A01>(); //ptr:1, ctrl_block:1
std::shared_ptr<A01> sec ;
sec = ptr; //这一步调用的哪个函数
return 1;
}
int main()
{
main444();
return 1;
}
sec = ptr;调用哪个?
见下面:
shared_ptr& operator=(const shared_ptr& _Right) noexcept
{ // assign shared ownership of resource owned by _Right
shared_ptr(_Right).swap(*this); //在swap之前,this所指向的控制块为NULL,所以临时shared_ptr对象在析构时,并不会对计数减1。所以此行结束之后,整体计数器加1
return (*this);
}