空结构背后的目的是什么? 从C++标准库声明AutoPPTR 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(); }; 名称空间std{ 模板结构auto_ptr_ref{}; 模板 自动类{ 公众: 类型定义X元素类型; //20.4.5.1建造/复制/销毁: 显式自动_ptr(X*p=0)throw(); auto_ptr(auto_ptr&)throw(); 模板自动_ptr(auto_ptr&)throw(); auto_ptr&运算符=(auto_ptr&)throw(); 模板auto_ptr&运算符=(auto_ptr&)throw(); auto_ptr&operator=(auto_ptr_ref)throw(); ~auto_ptr()throw(); //20.4.5.2成员: X&运算符*()常量throw(); X*运算符->()常量throw(); X*get()常量throw(); X*释放()抛出(); 无效重置(X*p=0)抛出(); //20.4.5.3转换: auto_ptr(auto_ptr_ref)throw(); 模板操作符auto_ptr_ref()throw(); 模板操作符auto_ptr()throw(); };

空结构背后的目的是什么? 从C++标准库声明AutoPPTR 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(); }; 名称空间std{ 模板结构auto_ptr_ref{}; 模板 自动类{ 公众: 类型定义X元素类型; //20.4.5.1建造/复制/销毁: 显式自动_ptr(X*p=0)throw(); auto_ptr(auto_ptr&)throw(); 模板自动_ptr(auto_ptr&)throw(); auto_ptr&运算符=(auto_ptr&)throw(); 模板auto_ptr&运算符=(auto_ptr&)throw(); auto_ptr&operator=(auto_ptr_ref)throw(); ~auto_ptr()throw(); //20.4.5.2成员: X&运算符*()常量throw(); X*运算符->()常量throw(); X*get()常量throw(); X*释放()抛出(); 无效重置(X*p=0)抛出(); //20.4.5.3转换: auto_ptr(auto_ptr_ref)throw(); 模板操作符auto_ptr_ref()throw(); 模板操作符auto_ptr()throw(); };,c++,pointers,C++,Pointers,} 我不明白这部分的目的: template <class Y> struct auto_ptr_ref {}; template struct auto_ptr_ref{}; 如果不声明任何变量,这些变量如何有效: auto_ptr& operator=(auto_ptr_ref<X>) throw(); auto_ptr&operator=(auto_ptr_ref)throw(); 还有这些: auto_pt

}

我不明白这部分的目的:

template <class Y> struct auto_ptr_ref {};
template struct auto_ptr_ref{};
如果不声明任何变量,这些变量如何有效:

auto_ptr&                      operator=(auto_ptr_ref<X>) throw();
auto_ptr&operator=(auto_ptr_ref)throw();
还有这些:

auto_ptr(auto_ptr_ref<X>) throw();
    template <class Y> operator auto_ptr_ref<Y>() throw();
auto_ptr(auto_ptr_ref)throw();
模板操作符auto_ptr_ref()throw();
编辑:而且(我只是注意到)我不明白最后两行是如何使用“运算符”的。语法不是类似于“return-type-operatoroperand;”,返回类型在哪里?操作数

谷歌搜索“auto_ptr_ref”显示


我不太明白这个解释,但看起来是为了下面的原因。如果没有这个技巧,您可以将一个
auto_ptr
传递给一个函数,该函数将获得对象的所有权,并将您的变量分配给空指针。使用上面的额外类技巧,在这种情况下,您将得到一个编译错误。

您已经从
auto\u ptr
上的wikipedia条目复制了文本,不是吗?这只是
auto_ptr
和co的公共接口,不是实现的摘录。他们将文章中的
auto_ptr_ref
正文留空,以表明库中没有为用户提供的注释

这里的最后两行:

 template <class Y> operator auto_ptr_ref<Y>() throw();
 template <class Y> operator auto_ptr<Y>() throw();
模板操作符auto_ptr_ref()throw();
模板操作符auto_ptr()throw();
是转换运算符。转换运算符的语法略有不同,因为声明converion运算符的返回类型(它已经是名称!)没有意义,所以您不需要编写
int operator int()但只是
运算符int()

如果您需要讨论auto_ptr_ref在auto_ref中是如何使用的,那么有两个。例如:

啊,我根本不知道这一点(对这里还是新手)@user385261:当你对一个网站不熟悉时,请阅读他们的常见问题解答。它不是用来踢的。