Exercise 13.28: Given the following classes, implement a default constructor and the necessary copy-control members.
(a)
class TreeNode {
private:
std::string value;
int count;
TreeNode *left;
TreeNode *right;
};
(b)
class BinStrTree {
private:
TreeNode *root;
};
如下類別,請試著實作出它們的預設建構器(default constructor)和必要的拷貝控制成員。
TreeNode.h
#ifndef TREENODE_H
#define TREENODE_H
#include<string>
class TreeNode
{
public:
TreeNode() : count(0), left(nullptr), right(nullptr), user_cntr(new size_t(1)) {};//預設建構器(default constructor)
TreeNode(const std::string& s) :value(s),
count(s.size()), left(new TreeNode[s.size()]), right(left + s.size() - 1), user_cntr(new size_t(1)) {}//預設建構器
//拷貝建構器
TreeNode(const TreeNode& tn) :
value(tn.value), count(tn.count), left(tn.left), right(tn.right),user_cntr(tn.user_cntr) {
++* user_cntr;
}
~TreeNode() {
if (-- * user_cntr == 0) {
delete[]left;
//left = nullptr; right = nullptr;
delete user_cntr;
}
}
TreeNode& operator=(const TreeNode& rhs) {
++* rhs.user_cntr;
if (--*user_cntr==0)
{
delete[]left;
//left = nullptr; right = nullptr;
delete user_cntr;
}
value = rhs.value;
count = rhs.count;
left = rhs.left;
right = rhs.right;
user_cntr = rhs.user_cntr;
return *this;
}
private:
std::string value;
int count;
TreeNode* left;//由此想到動態陣列(動態配置多個物件);再由new[]想到要解構delete[]
TreeNode* right;
size_t* user_cntr;
};
#endif // !TREENODE_H
BinStrTree.h
#ifndef BINSTRTREE_H
#define BINSTRTREE
#include"TreeNode.h"
class BinStrTree
{
public:
//BinStrTree():root(nullptr){}//預設建構器
BinStrTree(const std::string& s=std::string()):root(new TreeNode(s)),user_cntr(new size_t(1)){}
BinStrTree(TreeNode* tnp):root(tnp),user_cntr(new size_t(1)){}
//拷貝建構器
BinStrTree(const BinStrTree& bst) :root(bst.root), user_cntr(bst.user_cntr){++* user_cntr;}
//拷貝指定運算子
BinStrTree& operator=(const BinStrTree & bst ){
++*bst.user_cntr;
if (--*user_cntr==0)
{
delete root;
delete user_cntr;
}
root = bst.root;
user_cntr = bst.user_cntr;
return *this;
}
//解構器
~BinStrTree() {
if (-- * user_cntr == 0) {
delete root;
delete user_cntr;
}
}
private:
TreeNode* root;
size_t* user_cntr;
};
#endif // !BINSTRTREE_H
.cpp
#include<string>
#include"TreeNode.h"
#include"BinStrTree.h"
using namespace std;
int main() {
TreeNode tn,tn1("孫守真"),tn2(string("阿彌陀佛"));
tn = tn1;
TreeNode tn3(tn2);
tn = tn2;
tn1 = tn3;
BinStrTree bst("妙音如來"),bst1(bst),bst2,bstp(new TreeNode(tn3));
bst2 = bst;
bstp = bst2;
}
https://github.com/oscarsun72/prog1-C-Primer-5th-Edition-s-Exercises/tree/exercise13_28a/prog1
C++自修入門實境秀 542 重新譯撰 《C++ Primer 5th》
13.2 拷貝控制與資源管理 13.2.2. Defining Classes That Act Like Pointers Defining a Reference-Counted Class~ 練習13.28
上一集:
https://www.facebook.com/100003034306665/videos/2659180040859780
下一集:
全部:http://bit.ly/2NoA2ID 原檔下載:http://bit.ly/2Ixe2Vc
課文: http://bit.ly/2FIHV57
http://bit.ly/2mttmfa(第二篇)
第10-11章: http://bit.ly/2MuPmiZ
章12: http://bit.ly/2Rw53sH
重譯12章:http://bit.ly/2V8UgZ7
http://bit.ly/2G2fPSg (docx)
重譯11章:http://bit.ly/39P7HRU
第三篇:http://bit.ly/2UaFbDY
第三篇13章:http://bit.ly/33mh49y
https://play.google.com/books/reader?id=J1HMLyxqJfgC&pg=GBS.PT951.w.0.0.0.0.1
講義下載:
http://bit.ly/2khF8Ic (全部)
程式碼:
https://github.com/oscarsun72/prog1
緣起:http://bit.ly/2XwHOUH