#include #include class C2{ public: int data=4; ~C2(){ printf("C2 destroyed\n"); } }; class C1{ public: ~C1(){ printf("C1 destroyed\n"); } void PrintData(std::weak_ptrptr) { printf("Data is %d\n",ptr.lock()->data); } }; int main(){ std::shared_ptr ptrA{std::make_shared()}; std::weak_ptrptrA_2=ptrA; if (!ptrA_2.expired()) { printf("Count:%ld",ptrA.use_count()); std::shared_ptr ptrA_3{ptrA}; printf("Inside if statement.\n"); std::shared_ptr ptrB{std::make_shared()}; ptrA_2.lock()->PrintData(ptrB); printf("Count:%ld",ptrA.use_count()); } printf("Count:%ld",ptrA.use_count()); printf("Outside if statement.\n"); return -1; }