STL---auto_ptr实现
来源:互联网 发布:java怎么写日志 编辑:程序博客网 时间:2024/05/22 13:48
#include<iostream>
using namespace std;
template<class T>
class auto_ptr1
{
public:
explicit auto_ptr1(T* p =0 ):pointee(p){ cout<<"p..\n";}
template<class U>
auto_ptr1(auto_ptr1<U>&rhs):pointee(rhs.release()){cout<<"rhs..\n";}
~auto_ptr1()
{
cout<<"~\n";
delete pointee;
}
template<class U>
auto_ptr1<T>& operator=(auto_ptr1<U>&rhs)
{
cout<<"=\n";
if(this != &rhs)
reset(rhs.release());
return *this;
}
T& operator*()const
{
cout<<"*\n";
return *pointee;
}
T* operator->()const
{
cout<<"->\n";
return pointee;
}
T* get()const
{
return pointee;
}
//...
private:
T* pointee;
};
void test()
{
auto_ptr1<string> ps (new string("jjhou"));
cout<<*ps<<endl;
cout<<ps->size()<<endl;
}
int main()
{
test();
}
using namespace std;
template<class T>
class auto_ptr1
{
public:
explicit auto_ptr1(T* p =0 ):pointee(p){ cout<<"p..\n";}
template<class U>
auto_ptr1(auto_ptr1<U>&rhs):pointee(rhs.release()){cout<<"rhs..\n";}
~auto_ptr1()
{
cout<<"~\n";
delete pointee;
}
template<class U>
auto_ptr1<T>& operator=(auto_ptr1<U>&rhs)
{
cout<<"=\n";
if(this != &rhs)
reset(rhs.release());
return *this;
}
T& operator*()const
{
cout<<"*\n";
return *pointee;
}
T* operator->()const
{
cout<<"->\n";
return pointee;
}
T* get()const
{
return pointee;
}
//...
private:
T* pointee;
};
void test()
{
auto_ptr1<string> ps (new string("jjhou"));
cout<<*ps<<endl;
cout<<ps->size()<<endl;
}
int main()
{
test();
}
阅读全文
0 0
- STL---auto_ptr实现
- 智能指针(一):STL auto_ptr实现原理
- 智能指针(一):STL auto_ptr实现原理
- 智能指针(一):STL auto_ptr实现原理
- 智能指针(一):STL auto_ptr实现原理
- 智能指针(一):STL auto_ptr实现原理
- STL-auto_ptr
- STL auto_ptr
- auto_ptr预防作为STL容器元素的实现。
- STL中的auto_ptr指针
- stl智能指针auto_ptr
- 剖析STL auto_ptr源码
- STL--智能指针(auto_ptr)
- C++ STL 之 auto_ptr
- STL学习之auto_ptr
- stl智能指针auto_ptr
- auto_ptr STL智能指针
- 【STL】智能指针auto_ptr
- mysql用union代替or
- Linux/Android系统知识之Qcom Platform开机流程概述
- HDU 1247 Hat’s Words 经典字典树
- 【PAT】【Advanced Level】1053. Path of Equal Weight (30)
- python3 GUI开发 (2)
- STL---auto_ptr实现
- 沟通与性格
- thinkphp生成缩略图
- Struts2 搭建开发环境
- 日期
- if switch do while for分析
- JNI中javah命令的使用,生成.h的头文件
- Java线程如何实现同步?
- JVM---内存分配策略