#include <iostream>
#include <memory>
using namespace std;
struct MyClass
{
MyClass( ) { cout << "MyClass ctor runned." << endl; }
~MyClass( ) { cout << "MyClass dtor runned." << endl; }
} ;
int main( )
{
shared_ptr< MyClass> sp1( new MyClass) ;
cout << "Use count: " << sp1.use_count ( ) << endl;
shared_ptr< MyClass> sp2 = sp1;
cout << "Use count: " << sp1.use_count ( ) << endl;
sp1.reset ( ) ;
cout << "Use count: " << sp2.use_count ( ) << endl;
{
cout << "Entered inner block." << endl;
shared_ptr< MyClass> sp3 = sp2;
cout << "Use count: " << sp2.use_count ( ) << endl;
cout << "Leaving inner block." << endl;
}
cout << "Left inner block." << endl;
cout << "Use count: " << sp2.use_count ( ) << endl;
cout << "About to delete the last shared pointer." << endl;
sp2.reset ( ) ;
cout << "Deleted the last shared pointer." << endl;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnN0cnVjdCBNeUNsYXNzCnsKICBNeUNsYXNzKCkgeyBjb3V0IDw8ICJNeUNsYXNzIGN0b3IgcnVubmVkLiIgPDwgZW5kbDsgfQogIH5NeUNsYXNzKCkgeyBjb3V0IDw8ICJNeUNsYXNzIGR0b3IgcnVubmVkLiIgPDwgZW5kbDsgfQp9OwoKaW50IG1haW4oKSAKewogIHNoYXJlZF9wdHI8TXlDbGFzcz4gc3AxKG5ldyBNeUNsYXNzKTsKICBjb3V0IDw8ICJVc2UgY291bnQ6ICIgPDwgc3AxLnVzZV9jb3VudCgpIDw8IGVuZGw7CgogIHNoYXJlZF9wdHI8TXlDbGFzcz4gc3AyID0gc3AxOwogIGNvdXQgPDwgIlVzZSBjb3VudDogIiA8PCBzcDEudXNlX2NvdW50KCkgPDwgZW5kbDsKCiAgc3AxLnJlc2V0KCk7CiAgY291dCA8PCAiVXNlIGNvdW50OiAiIDw8IHNwMi51c2VfY291bnQoKSA8PCBlbmRsOwoKICB7CiAgICBjb3V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