forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
12.15.cpp
37 lines (30 loc) · 783 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
#include <memory>
#include <iostream>
struct destination {
unsigned id;
explicit destination(unsigned des_id = 0) : id(des_id) {}
}; // resource
struct connection {
destination *dest;
};
connection connect(destination *dest) {
connection new_conn;
new_conn.dest = dest;
std::cout << "Setup connection to " << new_conn.dest->id << std::endl;
return new_conn;
}
void disconnect(connection conn) {
std::cout << "Stop connection to " << conn.dest->id << std::endl;
//conn.dest = nullptr;
}
void f(destination &d) {
connection c = connect(&d);
std::shared_ptr<connection> p(&c, [](connection *p) { disconnect(*p); });
// use connection
std::cout << "Using connection " << c.dest->id << std::endl;
}
int main() {
destination d(5);
f(d);
return 0;
}