【C++】智能指针auto_ptr简单的实现
//【C++】智能指针auto_ptr简单的实现 #include <iostream>
using namespace std; template <class _Ty>
class auto_ptr
{
public:
auto_ptr(_Ty *_P = 0) :_Owns(_Ptr != 0), _Ptr(_P)
{}
auto_ptr<_Ty>(const auto_ptr <_Ty> &p):_Owns(p._Owns),_Ptr(p.release()) //拷贝构造
{}
auto_ptr<_Ty>& operator=(const auto_ptr<_Ty> &_Y) //赋值语句
{
if(this != &_Y)
{
if(_Ptr != _Y._Ptr)
{
if(_Owns)
delete _Ptr;
_Owns = _Y._Owns;
}
else
{
_Owns = _Y._Owns;
}
_Ptr = _Y.release();
}
return *this;
}
~auto_ptr()
{
if (_Owns)
{
delete _Ptr;
}
}
public:
_Ty* release()const //交换拥有者
{
((auto_ptr<_Ty>*const)this)->_Owns = false;
return _Ptr;
}
_Ty*get() const
{
return _Ptr;
}
_Ty& operator*() const
{
return (*get());
}
_Ty* operator->() const
{
return get();
} private:
_Ty *_Ptr;
bool _Owns;
}; int main()
{
int *p = new int(10);
auto_ptr<int> pa(p);
auto_ptr<int> pa1(pa);
auto_ptr<int> pa2=pa1;
cout<<*pa<<endl;
cout<<*pa1<<endl;
cout<<*pa2<<endl;
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
【C++】智能指针auto_ptr简单的实现的更多相关文章
- C++智能指针(auto_ptr)详解
智能指针(auto_ptr) 这个名字听起来很酷是不是?其实auto_ptr 只是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势,但也有其局限.本文总结的8个问题足 ...
- C++智能指针及其简单实现
本文将简要介绍智能指针shared_ptr和unique_ptr,并简单实现基于引用计数的智能指针. 使用智能指针的缘由 1. 考虑下边的简单代码: int main() { ); ; } 就如上边程 ...
- 智能指针auto_ptr & shared_ptr
转载:智能指针auto_ptr 很多人听说过标准auto_ptr智能指针机制,但并不是每个人都天天使用它.这真是个遗憾,因为auto_ptr优雅地解决了C++设计和编码中常见的问题,正确地使用它可以生 ...
- C++智能指针--auto_ptr指针
auto_ptr是C++标准库提供的类模板,头文件<memory>,auto_ptr对象通过初始化指向由new创建的动态内存,它是这块内存的拥有者,一块内存不能同一时候被分给两个拥有者.当 ...
- 自己动手实现智能指针auto_ptr
面试的时候,我们经常会被问到如何自己动手实现智能指针auto_ptr.今天我就一边参考STL库中的源代码,一边将auto_ptr的实现敲一遍. auto_ptr归根到底是一个模版类,那么这个类要实现哪 ...
- C++ 智能指针auto_ptr
template<class T> class auto_ptr { public: ); // Item M5 有“explicitfor”// 的描述 template<clas ...
- 关于智能指针auto_ptr
智能指针auto_ptr和shared_ptr也是面试中经常被问到的一个 感觉看auto_ptr的源码反而更加容易理解一些,因为源码的代码量并不大,而且比较容易理解. 本篇主要介绍auto_ptr 其 ...
- C++中的智能指针(auto_ptr)
实际上auto_ptr 仅仅是C++标准库提供的一个类模板,它与传统的new/delete控制内存相比有一定优势.使用它不必每次都手动调用delete去释放内存.当然有利也有弊,也不是全然完美的. 本 ...
- C++智能指针 auto_ptr
C++智能指针 auto_ptr auto_ptr 是一个轻量级的智能指针, 定义于 memory (非memory.h)中, 命名空间为 std. auto_ptr 适合用来管理生命周期比较短或者不 ...
随机推荐
- singleton pattern
1 normal mode class Singleton { private Singleton(){}; Singleton singleton; static Singleton getInst ...
- 【Python项目】配合爱漫画爬取漫画脚本而设计的GUI漫画阅读器 (一)
博客园的第一个坑,想想都有点小激动 =3= 首先是那个爬虫的地址: [原创]最近写的一个比较hack的小爬虫 选择工具: 以前用过Qt,那么选pyqt4也就是情理之中了. 明确需求: 0.首先,要读取 ...
- 【原创】用Python爬取LeetCode的AC代码到Github
在leetCode写了105道题高调膜科,考虑搬迁到自己的GitHub上,做成一个解题题库,面试的时候也可以秀一个 但是!但是! leetCode在线IDE的功能不要太舒服,我直接线上A了不少题,本地 ...
- html5移动开发--js温馨提示
1.a标签执行js笔试 <a id="myID" href="javascript:myfuction();"></a> 2.实时监听i ...
- javascript焦点图(能够自己主动切换 )
/* 思路总结: 1.实现图片滚动的function.鼠标经时候获取当前li的index.设置ndex自己主动递增的函数.实现淡入淡出效果的函数 2.整个实现效果一传递index为主线 3.我的编写代 ...
- “ddl”有一个无效 SelectedValue,因为它不在项目列表中。
“ddl_ekt”有一个无效 SelectedValue,因为它不在项目列表中. 怎么回事 现象: 在用户控件的page_load事件里绑定下拉框,报上面错误 解决: 将下拉框绑定,放在page_In ...
- 自动同步Android源代码的脚本(repo sync)
#!/bin/bash echo "================start repo sync====================" repo sync -j5 ]; do ...
- UVA 11388-GCD LCM(数学)
I I U C O N L I N E C Problem D: GCD LCM Input: standard input Output: standard output The GCD ...
- C++包括头文件<>和""差额
#include "book.h" #include<iostream.h> 在刚開始学习都会有这样的迷惑.有的程序用<>.有的却用"" ...
- [LeetCode62]Unique Paths
题目: A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). ...