diff --git a/ch13/README.md b/ch13/README.md index 889d5956..640a37de 100644 --- a/ch13/README.md +++ b/ch13/README.md @@ -181,3 +181,7 @@ Copy constructor and copy-assignment operator should dynamicly allocate memory f `StrBlob` is using smart pointers which can be managed with synthesized destructor, If an object of `StrBlob` is out of scope, the destructor for std::shared_ptr will be called automaticaly to free the memory dynamically allocated when the `use_count` goes to 0. ## Exercise 13.26 [hpp](ex13_26.h) | [cpp](ex13_26.cpp) + +## [Exercise 13.27](ex13_27.h) + +## Exercise 13.28 [hpp](ex13_28.h) | [cpp](ex13_28.cpp) diff --git a/ch13/ex13.27.cpp b/ch13/ex13.27.cpp deleted file mode 100644 index 378cfba1..00000000 --- a/ch13/ex13.27.cpp +++ /dev/null @@ -1,81 +0,0 @@ -/*************************************************************************** - * @file main.cpp - * @author Alan.W - * @date 01 JAN 2014 - * @remark - ***************************************************************************/ -//! -//! Exercise 13.27: -//! Define your own reference-counted version of HasPtr. -//! - -#include -#include - - - -//! for ex13.27 -class HasPtr -{ -public: - //! default constructor. - HasPtr(const std::string &s = std::string()): - ps(new std::string(s)), i(0), useCounter(new std::size_t(1)) { } - - //! copy constructor. - HasPtr(const HasPtr& rhs) : ps(rhs.ps), i(rhs.i), useCounter(rhs.useCounter) - { - ++*useCounter; - } - - - HasPtr& - operator = (const HasPtr& rhs); - - - ~HasPtr() - { - if(--*useCounter == 0) - { - delete ps; - delete useCounter; - } - } - -private: - std::string* ps; - int i; - std::size_t* useCounter; -}; - - - -int main() -{ - HasPtr hp("aaaaaa"), hp2; - HasPtr hp1(hp); - - hp2 = hp1; - - return 0; -} - - -inline HasPtr& -HasPtr::operator =(const HasPtr &rhs) -{ - ++*rhs.useCounter; - if(--*useCounter == 0) - { - delete useCounter; - delete ps; - } - - ps = rhs.ps; - useCounter = rhs.useCounter; - i = rhs.i; - - return *this; - -} - diff --git a/ch13/ex13_27.h b/ch13/ex13_27.h new file mode 100644 index 00000000..feb29967 --- /dev/null +++ b/ch13/ex13_27.h @@ -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 + +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 diff --git a/ch13/ex13_28.cpp b/ch13/ex13_28.cpp new file mode 100644 index 00000000..a8bef9d7 --- /dev/null +++ b/ch13/ex13_28.cpp @@ -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; +} diff --git a/ch13/ex13_28.h b/ch13/ex13_28.h new file mode 100644 index 00000000..a19ad6a5 --- /dev/null +++ b/ch13/ex13_28.h @@ -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 +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