auto_ptr
原创
©著作权归作者所有:来自51CTO博客作者yongkai的原创作品,请联系作者获取转载授权,否则将追究法律责任
应该这么用auto_ptr
#include <memory>
…
auto_ptr<TestClass> ap(new TestClass);//注意这里是调用的构造函数,不是=重载符
ap->Func();
或者
TestClass* p = new TestClass;
auto_ptr<TestClass) ap(p);
ap->Func();
千万不能这么用:
auto_ptr<TestClass> ap = new TestClass;//注意这里是=重载符
ap->Func()
个中原因,主要是要完成由new TestClass到auto_ptr<TestClass>的转换,auto_ptr没有这样的直接=重载,它误用auto_ptr_ref来完成这个转换,会造成错误。auto_ptr_ref设计的本意本不是这个。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
深入解析decltype和decltype(auto)
decltype关键字是C++11新标准引入的关键字,它和关键字auto的功能类似,也可以自动推导出给定表达式的类型,但它和auto的语法有些不同,这篇文章讲解了decltype的使用场景以及和auto不同的地方,同时也讲解了和auto结合使用的用法。
C++ C++14 C++ decltype C++ auto decltype(auto) -
auto_ptr中的auto_ptr_ref
先看看类auto_ptr的内部实现机制: template<typename _Tp> class auto_ptr { private: &nb
职场 休闲 构造函数 auto_ptr auto_ptr_ref -
std::auto_ptr简单使用
auto_ptr 为c++的智能指针,主要解决的问题是C++的内存泄露问题,但是本质的原因是智能指
#include 智能指针 c++ -
std::auto_ptr的原理及使用
使用auto_ptr作为成员变量,以避免资源泄漏。防
构造函数 析构函数 成员函数