-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.15.cpp
51 lines (40 loc) · 867 Bytes
/
12.15.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
48
49
50
51
#include <memory>
#include <iostream>
using std::shared_ptr;
using std::cout;
using std::endl;
struct destination{
int address;
};
struct connection{
bool active() const { return connected; }
int address;
bool connected = false;
};
connection connect(destination* d) {
connection c;
c.connected = true;
c.address = d->address;
return c;
}
void disconnect(connection *c) {
c->connected = false;
}
void end_connection(connection *c) {
disconnect(c);
}
void f(destination &d) {
connection c = connect(&d);
shared_ptr<connection> p(&c, [](connection *conn){ end_connection(conn); });
cout << (c.active() ? "connected to " : "not connected to ");
cout << c.address << endl;
p.reset();
cout << (c.active() ? "connected to " : "not connected to ");
cout << c.address << endl;
}
int main() {
destination d;
d.address = 420;
f(d);
return 0;
}