C++ 标准库中 auto_ptr 的声明
namespace std {
template <class Y> struct auto_ptr_ref {};
template <class X>
class auto_ptr {
public:
typedef X element_type;
// 20.4.5.1 construct/copy/destroy:
explicit auto_ptr(X* p =0) throw();
auto_ptr(auto_ptr&) throw();
template <class Y> auto_ptr(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr&) throw();
template <class Y> auto_ptr& operator=(auto_ptr<Y>&) throw();
auto_ptr& operator=(auto_ptr_ref<X>) throw();
~auto_ptr() throw();
// 20.4.5.2 members:
X& operator*() const throw();
X* operator->() const throw();
X* get() const throw();
X* release() throw();
void reset(X* p =0) throw();
// 20.4.5.3 conversions:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y> operator auto_ptr_ref<Y>() throw();
template <class Y> operator auto_ptr<Y>() throw();
};
}
我不明白这部分的目的:
template <class Y> struct auto_ptr_ref {};
如果不声明任何变量,这些如何有效:
auto_ptr& operator=(auto_ptr_ref<X>) throw();
还有这些:
auto_ptr(auto_ptr_ref<X>) throw();
template <class Y> operator auto_ptr_ref<Y>() throw();
Edit:而且(我只是注意到)我不明白最后两行如何使用“运算符”。语法不是类似“return-type operatoroperand;”,返回类型在哪里?操作数?
谷歌搜索“auto_ptr_ref”显示这个详细的解释 http://www.josuttis.com/libbook/auto_ptr.html.
我不太明白这个解释,但看起来是为了下面的内容。如果没有这个技巧,你可以通过auto_ptr
进入一个函数,该函数将获得该对象的所有权并将变量分配给空指针。在这种情况下,使用上面的额外类技巧,您将收到编译错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)