Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
weak_ptr and Circle_reference
#include <iostream> #include <memory> using namespace std; struct A; struct B; struct A{ weak_ptr<B> b; A (){ cout<<"A ctor"<<endl; } ~A(){ cout<<"A dtor"<<endl; } void SayHello(){ cout << "Hello I am A" << endl; } }; struct B{ weak_ptr<A> a; B (){ cout<<"B ctor"<<endl; } ~B(){ cout<<"B dtor"<<endl; } void CallA(){ auto sA = a.lock(); sA->SayHello(); } }; int main() { weak_ptr<A> wA; weak_ptr<B> wB; { shared_ptr<A> sA(new A()); shared_ptr<B> sB(new B()); sA->b = sB; sB->a = sA; wA = sA; wB = sB; sB->CallA(); cout << wA.use_count() << endl; // 1 cout << wB.use_count() << endl; // 1 } cout << wA.use_count() << endl; // 0 cout << wB.use_count() << endl; // 0 return 0; }
run
|
edit
|
history
|
help
2
c++ car racing game
NonparaRuns
Equilateral triangle
Depth of Bin tree
prime factorization trial division
Buenos Amigos
remove_copy_if-30-Seconds-of-C++
all possible palindrome partitions
sysTest.cpp
HTML Node