//unique_ptr需要将new操作符返回的指针传递给unique_ptr的构造函数
//unique_ptr无法进行拷贝构造和赋值操作,只能进行移动操作//独占他所指向的对象
//unique_ptr对象中保留指向某个对象的指针,当它本身被删除或者离开其作用域时会自动释放其指向对象所占用的资源
#include
#include
#include//auto_ptr unique_ptr
#include
using namespace std;
class Object
{
private:
mutable int a;//凡是由mutable修饰的属性都不受 const的限制
int value;
public:
Object(int x = 0) :value(x) { cout << “construct Object:” << endl; }
~Object() { cout << “deconstruct Object:” << endl; }
int& Value() { return value; }
const int& Value()const { return value; }
};
int main()
{
list<std::unique_ptr> objlist;//存放智能指针的list
objlist.push_back(unique_ptr(new Object(10)));//添加智能指针为对象
objlist.push_back(unique_ptr(new Object(20)));
objlist.push_back(unique_ptr(new Object(30)));
list<std::unique_ptr<Object>>::iterator it = objlist.begin();
for (; it != objlist.end(); ++it)
{
cout << (*it)->Value() << endl;
}
//当函数结束后 list销毁,销毁其存储的智能指针,智能指针被销毁后,其指向的资源被释放
return 0;
}
#if 0
//函数结束后,将容器的节点删除的同时,不能将其所指之物一起析构,因为无法判断其是对象还是指针
int main()
{
list<Object*> objlist;
objlist.push_back(new Object(10));
objlist.push_back(new Object(20));
objlist.push_back(new Object(30));
list<Object*>::iterator it = objlist.begin();
for (; it != objlist.end(); ++it)
{
cout << (*it)->Value() << endl;
}
}
void fun(unique_ptr x)
{
}
int main()
{
unique_ptrpobj(new Object(10));
unique_ptr<int[]>arr(new int[5]{ 1,2,3,4,5 });//指向数组的智能指针
//(pobj);//error,这将调动拷贝构造但是unique_ptr 无法进行拷贝构造
fun(std::move(pobj));//将pobj的资源给x
cout << “Main end” << endl;//资源的释放先于主函数
return 0;
}
unique_ptrfun(int x)
{
unique_ptrpobj(new Object(x));
return pobj;//调用移动构造
}
int main()
{
unique_ptr op;
op = fun(10);//调动移动赋值
if (op)
{
cout << op->Value() << endl;
}
return 0;
}
int main()
{
unique_ptrop(new Object(10));
if (op)//if语句中需要bool值
{
cout << op->Value() << endl;
}
op.reset(new Object(100));
Object* op1 = op.release();
unique_ptrop3(new Object(200));
//unique_ptrop4(std::move(op3));//移动构造//将op3的资源给op4
// op1 = std::move(op3);//std::move将op3变成右值
return 0;
}
//源码分析
#include
#include
#include
//面试重点
//使用智能指针时,必须有资源
//自动管理对象的生存周期
using namespace std;
class Object
{
private:
int value;
public:
Object(int x = 0) :value(x) { cout << “construct Object:” << x << endl; }
~Object() { cout << “deconstruct Object:” << endl; }
int& Value() { return value; }
const int& Value()const { return value; }
};
namespace lrd
{
template//模板类//template
struct default_delete//用结构体做默认删除器
{
void operator()(_Tp* ptr)const//重载()
{
delete ptr;
}
};
template<typename _Tp,typename _Dp=default_delete<_Tp>>//类参数,删除器参数
class unique_ptr
{
private:
_Tp* _Ptr;
unique_ptr(const unique_ptr&);//阻止产生缺省的拷贝构造
public:
typedef _Tp* pointer;
typedef _Tp element_type;
typedef _Dp delete_type;
delete_type get_delete() { return delete_type(); }//delete_type()是删除器,是一个对象,
public:
unique_ptr(_Tp* p = nullptr) :_Ptr(p) {}
~unique_ptr() {
if (_Ptr!- nullptr)
{
//delete _Ptr;
get_delete()(_Ptr);//第二个括号是重载过的
// delete_type()(_Ptr);//
}
_Ptr = nullptr;
}
//c11标准
unique_ptr(const unique_ptr&)=delete;//删除拷贝构造
unique_ptr& operator=(const unique_ptr&) = delete;//删除赋值
pointer get()const { return _Ptr; }
_Tp& operator*() { return *get(); }//返回所指之物
const _Tp& operator*()const { return *get(); }
//返回地址
pointer operator->()const { return get(); }
//重载bool指针
explicit operator bool()const { return _Ptr != nullptr; }//
void reset( _Tp* it=nullptr)
{
get_delete()(_Ptr);
_Ptr=it
}
//解除拥有权
pointer release()
{
pointer p = _Ptr;
_Ptr=nullptr;
return p;
}
void swap(unique_ptr& _X)
{
std::swap(this->_Ptr, _X._Ptr);
}
unique_ptr(unique_ptr&& _u)
{
_Ptr = _u._Ptr;
_u._Ptr = nullptr;
}
unique_ptr& operator=(unique_ptr&& _u)
{
if (this != _u)
{
reset(_u.release());
}
return *this;
}
};
}