forked from Mooophy/Cpp-Primer
-
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
5 changed files
with
141 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// | ||
// ex13_27.h | ||
// Exercise 13.27 | ||
// | ||
// Created by pezy on 1/20/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Define your own reference-counted version of HasPtr. | ||
|
||
#ifndef CP5_ex13_27_h | ||
#define CP5_ex13_27_h | ||
|
||
#include <string> | ||
|
||
class HasPtr { | ||
public: | ||
HasPtr(const std::string &s = std::string()) : ps(new std::string(s)), i(0), use(new size_t(1)) { } | ||
HasPtr(const HasPtr &hp) : ps(hp.ps), i(hp.i), use(hp.use) { ++*use; } | ||
HasPtr& operator=(const HasPtr &rhs) { | ||
++*rhs.use; | ||
if (--*use == 0) { | ||
delete ps; | ||
delete use; | ||
} | ||
ps = rhs.ps; | ||
i = rhs.i; | ||
use = rhs.use; | ||
return *this; | ||
} | ||
~HasPtr() { | ||
if (--*use == 0) { | ||
delete ps; | ||
delete use; | ||
} | ||
} | ||
private: | ||
std::string *ps; | ||
int i; | ||
size_t *use; | ||
}; | ||
|
||
#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,41 @@ | ||
// | ||
// ex13_28.cpp | ||
// Exercise 13.28 | ||
// | ||
// Created by pezy on 1/20/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Given the following classes, implement a default constructor and the necessary copy-control members. | ||
|
||
#include "ex13_28.h" | ||
|
||
TreeNode& TreeNode::operator=(const TreeNode &rhs) | ||
{ | ||
++*rhs.count; | ||
if (--*count == 0) { | ||
if (left) { | ||
delete left; | ||
left = nullptr; | ||
} | ||
if (right) { | ||
delete right; | ||
right = nullptr; | ||
} | ||
|
||
delete count; | ||
count = nullptr; | ||
} | ||
value = rhs.value; | ||
left = rhs.left; | ||
right = rhs.right; | ||
count = rhs.count; | ||
return *this; | ||
} | ||
|
||
BinStrTree& BinStrTree::operator=(const BinStrTree &bst) | ||
{ | ||
TreeNode *new_root = new TreeNode(*bst.root); | ||
delete root; | ||
root = new_root; | ||
return *this; | ||
} |
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,54 @@ | ||
// | ||
// ex13_28.h | ||
// Exercise 13.28 | ||
// | ||
// Created by pezy on 1/20/15. | ||
// Copyright (c) 2015 pezy. All rights reserved. | ||
// | ||
// Given the following classes, implement a default constructor and the necessary copy-control members. | ||
|
||
#ifndef CP5_ex13_28_h | ||
#define CP5_ex13_28_h | ||
|
||
#include <string> | ||
using std::string; | ||
|
||
class TreeNode { | ||
public: | ||
TreeNode() : value(string()), count(new int(1)), left(nullptr), right(nullptr) {} | ||
TreeNode(const TreeNode &rhs) : value(rhs.value), count(rhs.count), left(rhs.left), right(rhs.right) { ++*count; } | ||
TreeNode& operator=(const TreeNode &rhs); | ||
~TreeNode() { | ||
if (--*count == 0) { | ||
if (left) { | ||
delete left; | ||
left = nullptr; | ||
} | ||
if (right) { | ||
delete right; | ||
right = nullptr; | ||
} | ||
delete count; | ||
count = nullptr; | ||
} | ||
} | ||
|
||
private: | ||
std::string value; | ||
int *count; | ||
TreeNode *left; | ||
TreeNode *right; | ||
}; | ||
|
||
class BinStrTree { | ||
public: | ||
BinStrTree() : root(new TreeNode()) {} | ||
BinStrTree(const BinStrTree &bst) : root(new TreeNode(*bst.root)) {} | ||
BinStrTree& operator=(const BinStrTree &bst); | ||
~BinStrTree() { delete root; } | ||
|
||
private: | ||
TreeNode *root; | ||
}; | ||
|
||
#endif |