forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added 12.10~18 to README, and refactor 14,15,16
- Loading branch information
Showing
10 changed files
with
141 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters