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 ...
随机推荐
- [转]vs2010 快捷键大全
vs2010 快捷键大全 VS2010版快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + ...
- I.MX6 driver goto 使用
/************************************************************************** * I.MX6 driver goto 使用 * ...
- 编写高效的C程序与C代码优化 via jobbole
http://blog.jobbole.com/82582/ 原文出处: codeproject 译文出处:CodingWu的博客 欢迎分享原创到伯乐头条
- 嵌入式Linux USB WIFI驱动的移植
硬件平台:飞思卡尔MX258开发板 操作系统:Linux2.6.31 WIFI: RT2860 USB WIFI模组 交叉编译环境:gcc version 4.1.2 调试步骤: 第一步:测试U ...
- C# 网页图片爬虫的几种技术基础
一.文件流方式获取网络图片资源 方法1 , ); System.Net.WebRequest webreq = System.Net.WebRequest.Create(url); System.Ne ...
- Apache-AB压力测试实例
一 AB背景介绍 Apache附带的压力测试工具apache bench--简称ab,非常容易使用,并且完全可以摸你各种条件对Web服务器发起测试请求.ab可以直接在Web服务器本地发起测试请求,这对 ...
- Balanced Numbers(数位+状压)
题意:求给定区间,一个数的数位上每个奇数出现偶数次,每个偶数出现奇数次,这样数的个数 分析:先考虑状态,但总是想不全,所以要把状态压缩一下,用三进制,0 该数不放 1 放了奇数次 2放了偶数次 dp ...
- FZU 2129 子序列个数
Problem Description 子序列的定义:对于一个序列a=a[1],a[2],......a[n].则非空序列a'=a[p1],a[p2]......a[pm]为a的一个子序列,其中1& ...
- .Net高级技术
本次课程中讲的有的东西都是根据初学者的认知规律进行了调整,并不是严谨的,比如很多地方在多AppDomain条件下很多说法就不对了,但是说严谨了大家就晕了,因此继续不严谨的讲吧. 很多面试题都在这阶段的 ...
- bzoj 1095 [ZJOI2007]Hide 捉迷藏(括号序列+线段树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1095 [题意] 给定一棵树,树上颜色或白或黑而且可以更改,多个询问求最远黑点之间的距离 ...