boost::scoped_ptr与std::unique_ptr
生活随笔
收集整理的這篇文章主要介紹了
boost::scoped_ptr与std::unique_ptr
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
boost::scoped_ptr與std::unique_ptr都是類模板,封裝了指針
兩者都禁用了拷貝構(gòu)造和賦值函數(shù),因此不能作為STL容器中的元素,因?yàn)樵趫?zhí)行push_back()時(shí)需要調(diào)用賦值函數(shù)。
?
std::unique_ptr實(shí)際上與boost::scoped_ptr是等價(jià)的,只是std將boost::scoped_ptr拿來(lái)名字改為了std::unique_ptr,兩者的實(shí)現(xiàn)方式是一致的。 boost::scoped_ptr源碼如下:
template<class T> class scoped_ptr { private:ScopedPtr(const ScopedPtr<T>& ap);ScopedPtr<T>& operator=(const ScopedPtr<T>& ap);public:ScopedPtr(T* ptr = nullptr):_ptr(ptr){}~ScopedPtr(){if(_ptr){delete _ptr;_ptr = nullptr;}}private:T* _ptr; };C++11實(shí)現(xiàn)的std::unique_ptr以后,并沒(méi)有去實(shí)現(xiàn)std::unique_array,是因?yàn)闃?biāo)準(zhǔn)庫(kù)中有vector,其實(shí)是很好用的
總結(jié)
以上是生活随笔為你收集整理的boost::scoped_ptr与std::unique_ptr的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 算法(第四版)IDEA终极环境配置
- 下一篇: mysql 设置主键命令_MySQL常用