careercup-C和C++ 13.8
13.8 编写一个智能指针类。智能指针是一种数据类型,一般用模板实现,模拟指针行为的同时还提供自动垃圾回收机制。它会自动记录SmartPointer<T*>对象的引用计数,一旦T类型对象的引用计数为零,就会释放该对象。
解法:
智能指针跟普通指针一样,但他借助自动化内存管理保证了安全性,避免了诸如悬挂指针、内存泄漏和分配失败等问题。
智能指针必须为给定对象的所有引用维护单一引用计数。主要实现构造函数、复制构造函数和赋值运算符,析构函数。
C++实现代码:
#include<iostream>
#include<cstdlib>
#include<new>
using namespace std; template <typename T>
class SmartPointer
{
public:
SmartPointer(T* ptr)
{
ref=ptr;
ref_count=(unsigned*)malloc(sizeof(unsigned));
*ref=;
}
SmartPointer(SmartPointer<T> &sptr)
{
ref=sptr.ref;
ref_count=sptr.ref_count;
++(*ref_count);
}
SmartPointer<T> &operator=(SmartPointer<T> &sptr)
{
if(this==&sptr) return *this;
if(*ref_count>)
{
remove();
}
ref=sptr.ref;
ref_count=sptr.ref_count;
++(*ref_count);
return *this;
}
~SmartPointer()
{
remove();
} T getValue()
{
return *ref;
}
protected:
void remove()
{
--(*ref_count);
if(*ref_count==)
{
delete ref;
free(ref_count);
ref=NULL;
ref_count=NULL;
}
}
private:
T* ref;
unsigned *ref_count;
}; int main()
{
int *p1=new int();
*p1=;
int *p2=new int();
*p2=;
SmartPointer<int> sp1(p1),sp2(p2);
SmartPointer<int> spa=sp1;
sp2=spa;
}
careercup-C和C++ 13.8的更多相关文章
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- [CareerCup] 18.13 Largest Rectangle of Letters
18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...
- [CareerCup] 13.1 Print Last K Lines 打印最后K行
13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...
- [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map
13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...
- [CareerCup] 13.3 Virtual Functions 虚函数
13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...
- [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝
13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...
- [CareerCup] 13.5 Volatile Keyword 关键字volatile
13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...
- [CareerCup] 13.6 Virtual Destructor 虚析构函数
13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...
- [CareerCup] 13.7 Node Pointer 节点指针
13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...
- [CareerCup] 13.8 Smart Pointer 智能指针
13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...
随机推荐
- [Mac][$PATH]如何修改$PATH变量
从 stackoverflow 找到的方法 http://stackoverflow.com/questions/7703041/editing-path-variable-on-mac 首先打开终端 ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.7
The set of all invertible matrices is a dense open subset of the set of all $n\times n$ matrices. Th ...
- Codeforces 633D Fibonacci-ish 暴力
题意:1000个元素,每个元素的大小-1e9<=a[i]<=1e9,然后让你重新安排这些元素的位置 获得最长的前缀斐波那契数列 分析:枚举第一个元素和第二个元素,因为在题目元素的范围内,最 ...
- 黑盒测试用例设计方法&理论结合实际 -> 场景法
一概念 现在的软件几乎都是用事件触发来控制流程的,事件触发时的情景便形成了场景,而同一事件不同的触发顺序和处理结果就形成事件流.这种在软件设计方面的思想也可以引入到软件测试中,可以比较生动地描绘出事件 ...
- unity3d切换场景时,背景音乐保持播放
首先创建两个场景: One,Two 再创建一个空游戏对象: GameObject,并添加AudioSource组件,把要播放的音乐拖放进去 给GameObject添加脚本AlwayAudio,代码如下 ...
- opencv 在工业中的应用:二维标定
在工业中经常要检测一个零件的尺寸,但是图像处理得到的是像素值,怎么才能得到实际的毫米值呢?这就要用到二维标定,我用OPENCV写了一个利用标定板进行标定的DEMO. 很多商业软件都没有二维标定的功能, ...
- 《Oracle Database 12c DBA指南》第一章 - 基本技能简介
当前关于12c的中文资料比较少,本人将关于DBA的一部分官方文档翻译为中文,很多地方为了帮助中国网友看懂文章,没有按照原文句式翻译,翻译不足之处难免,望多多指正. 1 基本技能简介 作为一个数据库管理 ...
- php数组使用小结
在PHP中,数组分为两类:索引数组和关联数组.二者可以单独使用,也可以混合使用. 1.一维数组 一维数组的定义也很简单,常用的有如下两种方式: 1.1 直接赋值 1: <?php 3: $dwq ...
- Python中的导入
转自:http://bingotree.cn/?p=569 参考<Python学习手册>,强烈建议看下这本书的相关章节. 在一些规模较大的项目中,经常可以看到通过imp.__import_ ...
- 安装禅道项目管理软件ZenTaoPMS
服务器Ubuntu 13.04 且安装了上一篇随笔中的 AMP本文略去安装AMP过程.版本号满足要求(php>5.2 and mysql.2) 1.官网http://www.zentao.net ...