generated from sigonasr2/CPlusPlusProjectTemplate
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
720 B
40 lines
720 B
#include <memory>
|
|
#include <stdio.h>
|
|
|
|
class C2{
|
|
public:
|
|
int data=4;
|
|
~C2(){
|
|
printf("C2 destroyed\n");
|
|
}
|
|
};
|
|
|
|
class C1{
|
|
public:
|
|
~C1(){
|
|
printf("C1 destroyed\n");
|
|
}
|
|
void PrintData(std::weak_ptr<C2>ptr) {
|
|
printf("Data is %d\n",ptr.lock()->data);
|
|
}
|
|
};
|
|
|
|
|
|
int main(){
|
|
|
|
std::shared_ptr<C1> ptrA{std::make_shared<C1>()};
|
|
|
|
std::weak_ptr<C1>ptrA_2=ptrA;
|
|
if (!ptrA_2.expired()) {
|
|
printf("Count:%ld",ptrA.use_count());
|
|
std::shared_ptr<C1> ptrA_3{ptrA};
|
|
printf("Inside if statement.\n");
|
|
std::shared_ptr<C2> ptrB{std::make_shared<C2>()};
|
|
ptrA_2.lock()->PrintData(ptrB);
|
|
printf("Count:%ld",ptrA.use_count());
|
|
}
|
|
printf("Count:%ld",ptrA.use_count());
|
|
printf("Outside if statement.\n");
|
|
|
|
return -1;
|
|
}
|
|
|