13.2. Copy Control and Resource Management

//Exercises Section 13.2
//Exercise 13.22: Assume that we want HasPtr to behave like a value.That
//is, each object should have its own copy of the string to which the objects
//point.We’ll show the definitions of the copy - control members in the next
//section.However, you already know everything you need to know to
//implement these members.Write the HasPtr copy constructor and copyassignment
//operator before reading on.
//Exercises Section 13.2.1
//Exercise 13.23: Compare the copy - control members that you wrote for the
//solutions to the previous section’s exercises to the code presented here.Be
//sure you understand the differences, if any, between your code and ours.

class HasPtr {
public:
	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 &hp) {
		auto newp = new string(*hp.ps);
		delete ps;
		ps = newp;
		i = hp.i;
		return *this;
	}
	~HasPtr() {
		delete ps;
	}
private:
	string *ps;
	int i;
};

int main() {
	return 0;
}

//Exercise 13.24: What would happen if the version of HasPtr in this section
//didn¡¯t define a destructor ? What if HasPtr didn¡¯t define the copy
//constructor ?
//if didn't define a destructor, a memory leak would occur, compiler synthesized destructor doesn't manage dynamic memory.
//if didn't define the copy constructor, it would behave like pointer

//Exercises Section 13.2.2
//Exercise 13.27: Define your own reference - counted version of HasPtr.

class HasPtr {
public:
	HasPtr(const string &s, int i) : use(new size_t(1)), ps(new string(s)), i(i) { }
	HasPtr(const HasPtr &hp) {
		ps = hp.ps;
		i = hp.i;
		++*use;
	}

	HasPtr &operator=(HasPtr &hp) {
		++*hp.use;
		if (--*use == 0) {
			delete ps;
			delete use;
		}
		ps = hp.ps;
		i = hp.i;
		return *this;
	}

	~HasPtr() {
		if (--*use == 0) {
			delete ps;
			delete use;
		}
	}

private:
	size_t *use;
	string *ps;
	int i;
};

//Exercise 13.28: Given the following classes, implement a default constructor
//and the necessary copy - control members.
//(a)
class TreeNode {
public:
	TreeNode() : value(string()), count(new int(1)), left(nullptr), right(nullptr) { }
	TreeNode(const TreeNode &t) : value(t.value), count(t.count), left(t.left), right(t.right) { }
	TreeNode &operator=(const TreeNode &t) {
		++*t.count;
		if (--*t.count == 0) {
			delete left;
			delete right;
			delete count;
		}
		value = t.value;
		count = t.count;
		left = t.left;
		right = t.right;
		count = t.count;
		return *this;
	}
	~TreeNode() {
		if (--*count == 0) {
			delete left;
			delete right;
			delete count;
		}
	}
private:
	string value;
	int *count;
	TreeNode *left;
	TreeNode *right;
};
//(b)
class BinStrTree {
public:
	BinStrTree() : root(new TreeNode()) { }
	BinStrTree(const BinStrTree &bst) : root(new TreeNode(*bst.root)) { }
	BinStrTree &operator=(const BinStrTree &bst) {
		TreeNode *new_root = new TreeNode(*bst.root);
		delete root;
		root = new_root;
		return *this;
	}
	~BinStrTree() { delete root; }

private:
	TreeNode *root;
};

int main() {
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值