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 ...
随机推荐
- JSOI2007建筑抢修
实际上和大多这类题一样(比如wikioi上的地鼠游戏),考察的都是堆的操作 这次改完之后就算把堆的模版定下来了 悲剧的是:大根堆打成了小根堆,导致一开始一直是10分…… 按结束时间排序,(经过验证,结 ...
- [swustoj 1021] Submissions of online judge
Submissions of online judge(1021) 问题描述 An online judge is a system to test programs in programming c ...
- git提交小结
git有工作区和暂存区的概念,工作区就是可以看到文件目录的地方,暂存区则是提交代码的地方 第一步,进入文件工作目录,终端输入命令 $ dir1/dir2: 第二步,查看哪些文件已经修改,输入命令 $ ...
- 编写高效的C程序与C代码优化 via jobbole
http://blog.jobbole.com/82582/ 原文出处: codeproject 译文出处:CodingWu的博客 欢迎分享原创到伯乐头条
- CF GYM 100703M It's complicate
题意:龙要做茶,需要n种原料,给出他有的原料个数,和每份茶需要的原料个数,和每种原料的价格,要求做整数份茶,把他之前有的原料用完最少要花多少钱. 解法:水题. 代码: #include<stdi ...
- [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.5
Suppose it is known that $\scrM$ is an invariant subspace for $A$. What invariant subspaces for $A\o ...
- linux time命令参数--执行命令并计时
[命令]time — 执行命令并计时 [格式]time [-p] command [arguments...] [说明] 执行命令行"command [arguments...]" ...
- error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7
工程使用了DirectDraw,编译出错 error LNK2001: 无法解析的外部符号 _IID_IDirectDraw7 解决办法是吧dxguid.lib添加到工程中,把lib所在目录添加到工程 ...
- 2015年10月23日JS笔记
ECMAScript标准:JavaScript核心语法 微软:Jscript ECMAScript标准:一纸空文 JavaScript和JScritp都号称完全实现了 ECMAScript标准 W3C ...
- HW6.27
import java.util.Scanner; import java.util.Arrays; public class Solution { public static void main(S ...