Skip to content

Commit

Permalink
added 12.10~18 to README, and refactor 14,15,16
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 24, 2014
1 parent 5b54c6f commit 073472e
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 192 deletions.
9 changes: 8 additions & 1 deletion ch12/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,11 @@ Memory leakage happens. Because after `r = q` was executed, no pointer points to

- to `q2` and `r2`:

no memory leakages. As `r2 = q2` was executed, the count of the object `r2` had pointer to went to `0`, which freed the corresponding memory.
## [Exercise 12.10](ex12_10.h)
## [Exercise 12.11](ex12_11.cpp)
## [Exercise 12.12](ex12_12.cpp)
## [Exercise 12.13](ex12_13.cpp)
## [Exercise 12.14](ex12_14.cpp)
## [Exercise 12.15](ex12_15.cpp)
## [Exercise 12.16](ex12_16.cpp)
## [Exercise 12.17 and 12.18](ex12_17_18.cpp)
57 changes: 0 additions & 57 deletions ch12/ex12.14.cpp

This file was deleted.

66 changes: 0 additions & 66 deletions ch12/ex12.15.cpp

This file was deleted.

64 changes: 0 additions & 64 deletions ch12/ex12.16.cpp

This file was deleted.

File renamed without changes.
File renamed without changes.
53 changes: 53 additions & 0 deletions ch12/ex12_14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// ex12_14.cpp
// Exercise 12.14
//
// Created by pezy on 12/22/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Write your own version of a function that uses a shared_ptr to manage a connection.

#include <iostream>
#include <string>
#include <memory>

struct connection {
std::string ip;
int port;
connection(std::string ip_, int port_):ip(ip_), port(port_){ }
};
struct destination {
std::string ip;
int port;
destination(std::string ip_, int port_):ip(ip_), port(port_){ }
};

connection connect(destination* pDest)
{
std::shared_ptr<connection> pConn(new connection(pDest->ip, pDest->port));
std::cout << "creating connection(" << pConn.use_count() << ")" << std::endl;
return *pConn;
}

void disconnect(connection pConn)
{
std::cout << "connection close(" << pConn.ip << ":" << pConn.port << ")" << std::endl;
}

void end_connection(connection *pConn)
{
disconnect(*pConn);
}

void f(destination &d)
{
connection conn = connect(&d);
std::shared_ptr<connection> p(&conn, end_connection);
std::cout << "connecting now(" << p.use_count() << ")" << std::endl;
}

int main()
{
destination dest("202.118.176.67", 3316);
f(dest);
}
48 changes: 48 additions & 0 deletions ch12/ex12_15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ex12_15.cpp
// Exercise 12.15
//
// Created by pezy on 12/22/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Rewrite the first exercise to use a lambda (10.3.2, p.388) in place of the end_connection function.

#include <iostream>
#include <string>
#include <memory>

struct connection {
std::string ip;
int port;
connection(std::string ip_, int port_):ip(ip_), port(port_){ }
};
struct destination {
std::string ip;
int port;
destination(std::string ip_, int port_):ip(ip_), port(port_){ }
};

connection connect(destination* pDest)
{
std::shared_ptr<connection> pConn(new connection(pDest->ip, pDest->port));
std::cout << "creating connection(" << pConn.use_count() << ")" << std::endl;
return *pConn;
}

void disconnect(connection pConn)
{
std::cout << "connection close(" << pConn.ip << ":" << pConn.port << ")" << std::endl;
}

void f(destination &d)
{
connection conn = connect(&d);
std::shared_ptr<connection> p(&conn, [](connection *p){disconnect(*p);});
std::cout << "connecting now(" << p.use_count() << ")" << std::endl;
}

int main()
{
destination dest("202.118.176.67", 3316);
f(dest);
}
30 changes: 30 additions & 0 deletions ch12/ex12_16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// ex12_15.cpp
// Exercise 12.15
//
// Created by pezy on 12/22/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Compilers don’t always give easy-to-understand error messages if we attempt to
// copy or assign a unique_ptr. Write a program that contains these errors to see
// how your compiler diagnoses them.

#include <iostream>
#include <string>
#include <memory>

using std::string; using std::unique_ptr;

int main()
{
unique_ptr<string> p1(new string("pezy"));
// unique_ptr<string> p2(p1); // copy
// ^
// Error: Call to implicitly-deleted copy constructor of 'unique_ptr<string>'
//
// unique_ptr<string> p3 = p1; // assign
// ^
// Error: Call to implicitly-deleted copy constructor of 'unique_ptr<string>'
std::cout << *p1 << std::endl;
p1.reset(nullptr);
}
6 changes: 2 additions & 4 deletions ch12/ex12.17.18.cpp → ch12/ex12_17_18.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
//!
//! Exercise 12.18:
//! Why doesn’t shared_ptr have a release member?
// Because other shared_ptr that points the same object can still delete this
// Because other shared_ptr that points the same object can still delete this
// object.Thus, it's meaningless to provide this member
// more detail can be found a thread on Stack Overflow:
// http://stackoverflow.com/questions/1525764/how-to-release-pointer-from-boostshared-ptr
// http://stackoverflow.com/questions/1525764/how-to-release-pointer-from-boostshared-ptr


#include <iostream>
Expand Down Expand Up @@ -80,5 +80,3 @@ int main()

return 0;
}


0 comments on commit 073472e

Please sign in to comment.