《Cracking the Coding Interview》——第13章:C和C++——题目8
2014-04-25 20:27
题目:实现一个能够通过引用计数来实现自动回收数据的智能指针,用C++,不是java。
解法:这题真心牛,我的第一反应是发呆,因为对引用计数的了解仅限于这个名词,完全没办法建立模型。之后仔细把题解读了两遍之后,照样敲了一遍代码。然后边调试边阅读才有了些理解。引用计数有四点需要注意:1. 引用计数是个非负整数。2. 引用计数是对于一个实体变量的,所有指向这个实体的指针共用这个计数,所以就有了代码中的那种写法。3. 指针进行析构的时候,引用计数减一。4. 增加一个指针的时候,引用计数加一。
代码:
// 13.8 Implement a smart pointer class, which is capable of automatic garbage collection. Count of reference is the key to this problem.
// Answer:
// int *ref_count;
// referece count must be int *, instead of int. Think about why.
// The destructor function is called for (*ref_count) times, only at the last time the real data is freed.
#include <iostream>
using namespace std; template <class T>
class Pointer {
public:
Pointer(T *ptr) {
data = ptr;
ref_count = new int();
}; Pointer(Pointer<T> &p) {
data = p.data;
ref_count = p.ref_count;
++(*ref_count);
}; Pointer<T>& operator = (Pointer<T> &p) {
if (this == &p) {
// nothing to do.
return *this;
}
if (*ref_count > ) {
remove();
}
data = p.data;
ref_count = p.ref_count;
++(*ref_count); return *this;
}; T getData() {
return *data;
}; void setData(const T &val) {
*data = val;
}; ~Pointer() {
remove();
};
protected:
T *data;
int *ref_count; void remove() {
--(*ref_count);
if (*ref_count == ) {
// if the reference count becomes 0, the data is never to be found again.
// it must be freed.
delete data;
data = nullptr;
delete ref_count;
ref_count = nullptr;
}
};
}; int main()
{
int *ptr = new int();
Pointer<int> p1(ptr);
Pointer<int> p2(p1); cout << p1.getData() << endl;
p2.setData();
cout << p2.getData() << endl; return ;
}
《Cracking the Coding Interview》——第13章:C和C++——题目8的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- 《Cracking the Coding Interview》——第18章:难题——题目13
2014-04-29 04:40 题目:给定一个字母组成的矩阵,和一个包含一堆单词的词典.请从矩阵中找出一个最大的子矩阵,使得从左到右每一行,从上到下每一列组成的单词都包含在词典中. 解法:O(n^3 ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目6
2014-04-25 20:07 题目:为什么基类的析构函数必须声明为虚函数? 解法:不是必须,而是应该,这是种规范.对于基类中执行的一些动态资源分配,如果基类的析构函数不是虚函数,那么 派生类的析构 ...
- 《Cracking the Coding Interview》——第17章:普通题——题目13
2014-04-29 00:15 题目:将二叉搜索树展开成一个双向链表,要求这个链表仍是有序的,而且不能另外分配对象,就地完成. 解法:Leetcode上也有,递归解法. 代码: // 17.13 F ...
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
2014-04-25 20:47 题目:分配一个二维数组,尽量减少malloc和free的使用次数,要求能用a[i][j]的方式访问数据. 解法:有篇文章讲了六种new delete二维数组的方式,其 ...
随机推荐
- WINCC runtime连接SIMOTION simulator SIMOSIM
测试使用的软件版本 TIA Portal V14sp1 Windows7 sp1 (professional) Scout 5.1(integrated in TIA 集成项目) VMware wor ...
- pat甲级1107
1107 Social Clusters (30 分) When register on a social network, you are always asked to specify your ...
- MySQL数据库实验四:嵌套查询
实验四 嵌套查询 一.实验目的 掌握SELECT语句的嵌套使用,实现表的复杂查询,进一步理解SELECT语句的高级使用方法. 二.实验环境 三.实验示例 1. 查询与“刘晨”在同一 ...
- CRUD全栈式编程架构之控制器的设计
页面 这里界面我采用jquery miniui来做的,当你完全了解了整个设计之后可以轻松切换到其他的js框架,个人认为类似muniui,easyui等等这类可以将web界面做得和winform类似的框 ...
- Java中调用MatLab返回值
当在Java中使用MatLab函数时,由于语言语法的不同,Matlab返回多个数据时,想在Java中获取到并进行使用.查阅了网上资料,翻箱倒柜加上自己实战,得出方法如下: 如MatLab函数返回的是N ...
- DOM(十四):代理检测和事件处理(跨浏览器)
一.检测 用于用户代理检测,检测范围包括浏览器引擎.平台.Windows.移动设备和游戏系统等 /* *用户代理检测脚本,检测范围包括浏览器引擎.平台.Windows.移动设备和游戏系统 */ var ...
- css3阴影 box-shadow
语法 box-shadow:X轴偏移量 y轴偏移量 [阴影模糊半径] [阴影扩展半径] [阴影颜色] [投影方式] 参数介绍: 注:inset 可以写在参数的第一个或最后一个,其它位置是无效的. 阴影 ...
- 剑指offer40
class Solution { public: void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) { ) ret ...
- 虚方法(virsual method)
虚方法(virsual method)挺起来玄乎其玄,向从未听说过这个概念的人解释清楚是一件相当困难的事情. 因为这是一个很不容易理解的概念,但它在比较抽象的代码里边是不可少的. 那么既然用枯燥的文字 ...
- python的**和*
1.**两个乘号就是乘方,比如2**4,结果就是2的4次方,结果是16一个乘号*,如果操作数是两个数字,就是这两个数字相乘,如2*4,结果为8*如果是字符串.列表.元组与一个整数N相乘,返回一个其所有 ...