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.
- Loading branch information
Showing
7 changed files
with
130 additions
and
106 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 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 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 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 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,49 @@ | ||
// | ||
// ex13_30.h | ||
// Exercise 13.30 | ||
// | ||
// Created by pezy on 1/23/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Write and test a swap function for your valuelike version of HasPtr. | ||
// Give your swap a print statement that notes when it is executed. | ||
// | ||
// See ex13_22.h | ||
|
||
#ifndef CP5_ex13_11_h | ||
#define CP5_ex13_11_h | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr { | ||
public: | ||
friend void swap(HasPtr&, HasPtr&); | ||
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } | ||
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } | ||
HasPtr& operator=(const HasPtr &hp) { | ||
auto new_p = new std::string(*hp.ps); | ||
delete ps; | ||
ps = new_p; | ||
i = hp.i; | ||
return *this; | ||
} | ||
~HasPtr() { | ||
delete ps; | ||
} | ||
|
||
void show() { std::cout << *ps << std::endl; } | ||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
void swap(HasPtr& lhs, HasPtr& rhs) | ||
{ | ||
using std::swap; | ||
swap(lhs.ps, rhs.ps); | ||
swap(lhs.i, rhs.i); | ||
std::cout << "call swap(HasPtr& lhs, HasPtr& rhs)" << std::endl; | ||
} | ||
|
||
#endif |
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,57 @@ | ||
// | ||
// ex13_31.h | ||
// Exercise 13.31 | ||
// | ||
// Created by pezy on 1/23/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Give your class a < operator and define a vector of HasPtrs. | ||
// Give that vector some elements and then sort the vector. | ||
// Note when swap is called. | ||
// | ||
// See ex13_30.h | ||
|
||
#ifndef CP5_ex13_11_h | ||
#define CP5_ex13_11_h | ||
|
||
#include <string> | ||
#include <iostream> | ||
|
||
class HasPtr { | ||
public: | ||
friend void swap(HasPtr&, HasPtr&); | ||
friend bool operator<(const HasPtr &lhs, const HasPtr &rhs); | ||
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0) { } | ||
HasPtr(const HasPtr &hp) : ps(new std::string(*hp.ps)), i(hp.i) { } | ||
HasPtr& operator=(HasPtr tmp) { | ||
this->swap(tmp); | ||
return *this; | ||
} | ||
~HasPtr() { | ||
delete ps; | ||
} | ||
|
||
void swap(HasPtr &rhs) { | ||
using std::swap; | ||
swap(ps, rhs.ps); | ||
swap(i, rhs.i); | ||
std::cout << "call swap(HasPtr &rhs)" << std::endl; | ||
} | ||
|
||
void show() { std::cout << *ps << std::endl; } | ||
private: | ||
std::string *ps; | ||
int i; | ||
}; | ||
|
||
void swap(HasPtr& lhs, HasPtr& rhs) | ||
{ | ||
lhs.swap(rhs); | ||
} | ||
|
||
bool operator<(const HasPtr &lhs, const HasPtr &rhs) | ||
{ | ||
return *lhs.ps < *rhs.ps; | ||
} | ||
|
||
#endif |