-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcyclicReference.cpp
47 lines (42 loc) · 1.04 KB
/
cyclicReference.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <memory>
struct Son;
struct Daughter;
struct Mother{
~Mother(){
std::cout << "Mother gone" << '\n';
}
void setSon(const std::shared_ptr<Son> s ){
mySon = s;
}
void setDaughter(const std::shared_ptr<Daughter> d ){
myDaughter = d;
}
std::shared_ptr<const Son> mySon;
std::weak_ptr<const Daughter> myDaughter;
};
struct Son{
Son(std::shared_ptr<Mother> m):myMother(m){}
~Son(){
std::cout << "Son gone" << '\n';
}
std::shared_ptr<const Mother> myMother;
};
struct Daughter{
Daughter(std::shared_ptr<Mother> m):myMother(m){}
~Daughter(){
std::cout << "Daughter gone" << '\n';
}
std::shared_ptr<const Mother> myMother;
};
int main(){
std::cout << '\n';
{
std::shared_ptr<Mother> mother = std::shared_ptr<Mother>( new Mother);
std::shared_ptr<Son> son = std::shared_ptr<Son>( new Son(mother) );
std::shared_ptr<Daughter> daughter = std::shared_ptr<Daughter>( new Daughter(mother) );
mother->setSon(son);
mother->setDaughter(daughter);
}
std::cout << '\n';
}