-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_14.cpp
41 lines (39 loc) · 996 Bytes
/
12_14.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
#include <iostream>
#include <memory>
#include <string>
using namespace std;
struct destination
{
string ip;
string port;
destination(string ip_, string port_):ip(ip_),port(port_){}
};
struct connection
{
string ip;
string port;
connection(string ip_, string port_):ip(ip_),port(port_){}
};
connection connect(destination *pDest)
{
shared_ptr<connection> pCon(new connection(pDest->ip, pDest->port));
cout << "creating connection(" << pCon->ip<<":"<<pCon->port<<")" << endl;
return *pCon;
}
void disconnect(connection Con)
{
cout << "connection close(" << Con.ip << ":" << Con.port << ")" << endl;
}
void end_connection(connection *p) { disconnect(*p); }
void f(destination &d)
{
connection c = connect(&d);
shared_ptr<connection> p(&c, end_connection);
cout << "connecting now(" << p.use_count() << ")" << endl;
}
int main()
{
destination dest("192.168.32.3", "8080");
f(dest);
return 0;
}