您的位置:首页 > 其它

auto_ptr_ref的奇妙(下)

2003-06-17 11:04 253 查看

auto_ptr_ref的奇妙(下)

在我们前面谈到的auto_ptr,它的复制操作的参数类型恰好是非常量引用。所以对于下面的情况它就不能正确处理。
auto_ptr<int> ap1 = auto_ptr<int>(new int(8));//等号右边的是一个临时右值
auto_ptr<int> fun()//一个生成auto_ptr<int>的source函数
{return auto_ptr<int>(new int(8))}
auto_ptr<int> ap2 ( fun() );//调用fun()生成的auto_ptr<int>是右值
而这种情况不但合法,也是很常见的,我们不能拒绝这种用法,怎么办?天才的C++库设计者已经为我们想到了这一点,auto_ptr_ref的引入就是为了解决这个问题。仔细观察最下面的auto_ptr实现代码,我们会看到这样几行:
/* special conversions with auxiliary type to enable copies and assignments */
auto_ptr(auto_ptr_ref<T> rhs) throw()
: ap(rhs.yp) {
}
auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new
reset(rhs.yp);
return *this;
}
template<class Y>
operator auto_ptr_ref<Y>() throw() {
return auto_ptr_ref<Y>(release());
}

这就是关键所在了。对于一个auto_ptr右值,不可能为它调用正常的赋值运算符函数和复制构造函数,举个例子说明,对于语句
auto_ptr<int> ap1 = auto_ptr<int>(new int(8));
首先生成临时对象右值auto_ptr<int>(new int(8)),然后使用转型函数模板
template<class Y> operator auto_ptr_ref<Y>() throw()
{ return auto_ptr_ref<Y>(release());};
由auto_ptr<int>(new int(8)),首先调用成员函数release(),然后由获取的原始指针生成另外一个临时对象右值auto_ptr_ref<int>(一个指向动态存储区中8的指针)。这个时候我们再看一个构造函数
auto_ptr(auto_ptr_ref<T> rhs) throw()
: ap(rhs.yp) {
}
它的参数类型不是引用,采用的是传值(by value),所以可以接受右值,这时候,调用auto_ptr_ref<int>默认生成的复制构造函数(copy constructor),用上面最后得到的那个auto_ptr_ref<int>临时对象右值作为引数,生成了rhs,接着auto_ptr_ref<int>临时对象右值自动析构结束生命,后面的ap(rhs.yp)完成最后的工作。
我们的整个探险过程就结束了。最好参考《The C++ Standard Library》中讲解auto_ptr使用的章节一起阅读。这样auto_ptr的使用和所有设计原理对我们就不再神秘了。
附录:auto_ptr的实现代码,来自Nicolai M. Josuttis
/* class auto_ptr
* - improved standard conforming implementation
*/
namespace std {
// auxiliary type to enable copies and assignments (now global)
template<class Y>
struct auto_ptr_ref {
Y* yp;
auto_ptr_ref (Y* rhs)
: yp(rhs) {
}
};

template<class T>
class auto_ptr {
private:
T* ap; // refers to the actual owned object (if any)
public:
typedef T element_type;

// constructor
explicit auto_ptr (T* ptr = 0) throw()
: ap(ptr) {
}

// copy constructors (with implicit conversion)
// - note: nonconstant parameter
auto_ptr (auto_ptr& rhs) throw()
: ap(rhs.release()) {
}
template<class Y>
auto_ptr (auto_ptr<Y>& rhs) throw()
: ap(rhs.release()) {
}

// assignments (with implicit conversion)
// - note: nonconstant parameter
auto_ptr& operator= (auto_ptr& rhs) throw() {
reset(rhs.release());
return *this;
}
template<class Y>
auto_ptr& operator= (auto_ptr<Y>& rhs) throw() {
reset(rhs.release());
return *this;
}

// destructor
~auto_ptr() throw() {
delete ap;
}

// value access
T* get() const throw() {
return ap;
}
T& operator*() const throw() {
return *ap;
}
T* operator->() const throw() {
return ap;
}

// release ownership
T* release() throw() {
T* tmp(ap);
ap = 0;
return tmp;
}

// reset value
void reset (T* ptr=0) throw() {
if (ap != ptr) {
delete ap;
ap = ptr;
}
}

/* special conversions with auxiliary type to enable copies and assignments
*/
auto_ptr(auto_ptr_ref<T> rhs) throw()
: ap(rhs.yp) {
}
auto_ptr& operator= (auto_ptr_ref<T> rhs) throw() { // new
reset(rhs.yp);
return *this;
}
template<class Y>
operator auto_ptr_ref<Y>() throw() {
return auto_ptr_ref<Y>(release());
}
template<class Y>
operator auto_ptr<Y>() throw() {
return auto_ptr<Y>(release());
}
};
}
吴桐写于2003.6.16
最近修改2003.6.16
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: