Skip to content

Commit 01b63f8

Browse files
author
mikeblome
committed
fixes 1486 weak pointer lock example
1 parent 23de981 commit 01b63f8

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

docs/cpp/codesnippet/CPP/how-to-create-and-use-weak-ptr-instances_1.cpp

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,77 +6,73 @@
66

77
using namespace std;
88

9-
class Controller
9+
class Controller
1010
{
1111
public:
12-
int Num;
13-
wstring Status;
14-
vector<weak_ptr<Controller>> others;
15-
explicit Controller(int i) : Num(i) , Status(L"On")
16-
{
17-
wcout << L"Creating Controller" << Num << endl;
18-
}
12+
int Num;
13+
wstring Status;
14+
vector<weak_ptr<Controller>> others;
15+
explicit Controller(int i) : Num(i), Status(L"On")
16+
{
17+
wcout << L"Creating Controller" << Num << endl;
18+
}
1919

20-
~Controller()
21-
{
22-
wcout << L"Destroying Controller" << Num << endl;
23-
}
20+
~Controller()
21+
{
22+
wcout << L"Destroying Controller" << Num << endl;
23+
}
2424

25-
// Demonstrates how to test whether the
26-
// pointed-to memory still exists or not.
27-
void CheckStatuses() const
28-
{
29-
for_each(others.begin(), others.end(), [] (weak_ptr<Controller> wp)
30-
{
31-
try
32-
{
33-
auto p = wp.lock();
34-
wcout << L"Status of " << p->Num << " = " << p->Status << endl;
35-
}
36-
37-
catch (bad_weak_ptr b)
38-
{
39-
wcout << L"Null object" << endl;
40-
}
41-
});
42-
}
25+
// Demonstrates how to test whether the
26+
// pointed-to memory still exists or not.
27+
void CheckStatuses() const
28+
{
29+
for_each(others.begin(), others.end(), [](weak_ptr<Controller> wp) {
30+
auto p = wp.lock();
31+
if (p)
32+
{
33+
wcout << L"Status of " << p->Num << " = " << p->Status << endl;
34+
}
35+
else
36+
{
37+
wcout << L"Null object" << endl;
38+
}
39+
});
40+
}
4341
};
4442

4543
void RunTest()
4644
{
47-
vector<shared_ptr<Controller>> v {
48-
make_shared<Controller>(0),
49-
make_shared<Controller>(1),
50-
make_shared<Controller>(2),
51-
make_shared<Controller>(3),
52-
make_shared<Controller>(4),
53-
};
45+
vector<shared_ptr<Controller>> v{
46+
make_shared<Controller>(0),
47+
make_shared<Controller>(1),
48+
make_shared<Controller>(2),
49+
make_shared<Controller>(3),
50+
make_shared<Controller>(4),
51+
};
5452

55-
// Each controller depends on all others not being deleted.
56-
// Give each controller a pointer to all the others.
57-
for (int i = 0 ; i < v.size(); ++i)
58-
{
59-
for_each(v.begin(), v.end(), [&v,i] (shared_ptr<Controller> p)
60-
{
61-
if(p->Num != i)
62-
{
63-
v[i]->others.push_back(weak_ptr<Controller>(p));
64-
wcout << L"push_back to v[" << i << "]: " << p->Num << endl;
65-
}
66-
});
67-
}
53+
// Each controller depends on all others not being deleted.
54+
// Give each controller a pointer to all the others.
55+
for (int i = 0; i < v.size(); ++i)
56+
{
57+
for_each(v.begin(), v.end(), [&v, i](shared_ptr<Controller> p) {
58+
if (p->Num != i)
59+
{
60+
v[i]->others.push_back(weak_ptr<Controller>(p));
61+
wcout << L"push_back to v[" << i << "]: " << p->Num << endl;
62+
}
63+
});
64+
}
6865

69-
for_each(v.begin(), v.end(), [](shared_ptr<Controller>& p)
70-
{
71-
wcout << L"use_count = " << p.use_count() << endl;
72-
p->CheckStatuses();
73-
});
66+
for_each(v.begin(), v.end(), [](shared_ptr<Controller> &p) {
67+
wcout << L"use_count = " << p.use_count() << endl;
68+
p->CheckStatuses();
69+
});
7470
}
7571

7672
int main()
77-
{
78-
RunTest();
79-
wcout << L"Press any key" << endl;
80-
char ch;
81-
cin.getline(&ch, 1);
73+
{
74+
RunTest();
75+
wcout << L"Press any key" << endl;
76+
char ch;
77+
cin.getline(&ch, 1);
8278
}

0 commit comments

Comments
 (0)