#include <iostream>
#include <memory>
class Object
{
public :
//...potentially lots of data...
} ;
class ObjectHandle
{
public :
ObjectHandle( ) { std:: cout << "Constructed" << std:: endl ; }
~ObjectHandle( ) { std:: cout << "Destructed" << std:: endl ; }
private :
Object * ptrToPotentiallyLotsOfData = nullptr;
} ;
int main( )
{
std:: cout << "ObjectHandle size: " << sizeof ( ObjectHandle) << std:: endl ;
std:: cout << ( std:: is_pod < ObjectHandle> :: value ? "Is plain-ol`-data" : "Is not plain-old-data" ) << std:: endl ;
std:: cout << "\n Outter scope began" << std:: endl ;
{
std:: weak_ptr < ObjectHandle> objectWPtr;
std:: cout << "Inner scope began" << std:: endl ;
{
std:: shared_ptr < ObjectHandle> objectSPtr = std:: make_shared < ObjectHandle> ( ) ;
objectWPtr = objectSPtr;
}
std:: cout << "Inner scope ended (shared_ptr goes out of scope)" << std:: endl ;
}
std:: cout << "Outter scope ended (weak_ptr goes out of scope)" << std:: endl ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8bWVtb3J5PgoKY2xhc3MgT2JqZWN0CnsKcHVibGljOgkKICAgLy8uLi5wb3RlbnRpYWxseSBsb3RzIG9mIGRhdGEuLi4KfTsKCmNsYXNzIE9iamVjdEhhbmRsZQp7CnB1YmxpYzoJCiAgIE9iamVjdEhhbmRsZSgpIHsgc3RkOjpjb3V0IDw8ICJDb25zdHJ1Y3RlZCIgPDwgc3RkOjplbmRsOyB9CiAgIH5PYmplY3RIYW5kbGUoKSB7IHN0ZDo6Y291dCA8PCAiRGVzdHJ1Y3RlZCIgPDwgc3RkOjplbmRsOyB9CiAgIApwcml2YXRlOgogICAgT2JqZWN0ICpwdHJUb1BvdGVudGlhbGx5TG90c09mRGF0YSA9IG51bGxwdHI7Cn07CgppbnQgbWFpbigpCnsKCXN0ZDo6Y291dCA8PCAiT2JqZWN0SGFuZGxlIHNpemU6ICIgPDwgc2l6ZW9mKE9iamVjdEhhbmRsZSkgPDwgc3RkOjplbmRs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