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 ...
随机推荐
- [LOJ 1027] Dangerous Maze
A - A Dangerous Maze Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- 剑指Offer:第一个只出现一次的字符
题目:在字符串中找出第一个只出现一次的字符.如输入"abaccdeff",这输出'b' // 第一个只出现一次的字符 #include <stdio.h> char f ...
- 先前设定的sa密码忘记了,如何修改sa密码?
在window身份验证登陆后,新建查询,输入以下代码就可以修改sa密码了: use master go exec sp_password null,'123456','sa' go
- 关于Memcache mutex设计模式的.net实现
之前在网上看过memcache-mutex的场景分析和实现代码,这里将.net方式加以实现,当然这里主要是依据原文的伪代码照猫画虎,以此做为总结及记录.如果您对相应实现感兴趣可以尝试使用本文提供的代码 ...
- MVC Action Filter
ASP.NET MVC Framework支持四种不同类型的Filter: Authorization filters – 实现IAuthorizationFilter接口的属性. Action fi ...
- C# 中winform的一些属性设置
1 窗体的大小固定住,不能调整其大小 窗体FormBorderStyle 属性设置为 FixedSingle; MaximizeBox 属性设置为false; MinimizeBox 属性设置为 ...
- Appium原理
Appium原理小结 Api接口调用selenium的接口,android底层用android的instrumentation(API2.3+ 通过绑定另外一个独立的selendroid项目来实现的) ...
- 黑盒测试用例设计方法&理论结合实际 -> 因果图法
一. 概念 因果图法是一种利用图解法分析输入的各种组合情况,从而设计测试用例的方法,它适合于检查程序输入条件的各种组合情况 二. 因果图法的应用 等价类划分法和边界值分析方法都是着重考虑输入条件,但没 ...
- Zabbix探索:Agent配置中Hostname错误引起的Agent.Ping报错
搭好了Zabbix_Server以后,添加了服务器本身和一台Windows的机器做测试,居然有这样的报警. Zabbix agent on zabbix_client is unreachable f ...
- css 横向渐变 图片阴影效果 字体模糊效果
(必须加在Table的TD里,如果TD有背景就会遮盖阴影,可以把背景放到外套的表格里去) 背景颜色渐变 横向渐变 style="filter:progid:DXImageTransform. ...