0

首先请确保您知道 std::auto_ptr 曾经有 3 个版本。

std::auto_ptr 的第一个版本如下所示:

template<class T>
class auto_ptr 
{
public:
    explicit auto_ptr(T *p = 0): pointee(p) {}

    // copy constructor member
    // template: initialize a new auto_ptr with any compatible auto_ptr
    template<class U> auto_ptr(auto_ptr<U>& rhs): pointee(rhs.release()) {}

    ~auto_ptr() { delete pointee; }

    // assignment operator
    // member template assign from any compatible auto_ptr 
    template<class U> auto_ptr<T>& operator=(auto_ptr<U>& rhs)
    {
        if (&rhs != this)
        {
            reset(rhs.release());
        }
        return *this;
    }

    T& operator*() const { return *get(); }
    T* operator->() const { return get(); } 

    // return value of current dumb pointer
    T* get() const { return pointee; } 

    // relinquish ownership of current dumb pointer and return its value
    T* release() { T* p = pointee;  pointee = 0; return p; }                            

    // delete owned pointer,assume ownership of p
    void reset(T *p = 0)
    { 
        if (p != pointee) 
        {
            delete pointee;
            pointee = p;
        }
    }
private:
    T* pointee;
};      

只有接口,而实现很容易。

我的代码是这样的:

auto_ptr<int> foo()
{
    auto_ptr<int> p(new int(1));
    return p;
}

int main()
{
    auto_ptr<int> p;
    p = foo();
    return 0;
}

在我看来,我的测试代码无法通过编译器。但是它通过了,当我运行它时,它因为删除一个指针而损坏了两次。

我跟踪汇编代码,发现如下流程,内存地址简称为低16位。

ctor: f8e4    
new: 6bf0
ctor: f7d4

copy ctor: f7d4  ->  f80c
dctor: f7d4 (NULL)
delete: 0

lea ecx, [ebp-0ECh] // f8e4: memory-> 6bf0

dctor: f80c (6bf0)
delete: 6bf0

dctor: f8e4 (6bf0)   // twice delete

似乎代码: p = foo(); ctor 一个临时对象,它保存 foo() 中的新内存。

关键是,为什么 p = foo() 只是改变 p.pointee,而不是调用 p.operator=() ?

我添加了第一个 auto_ptr 的实现。


与网友交谈,他指出mybe编译器生成

auto_ptr<T>& operator=(auto_ptr<T>& rhs)

除了使用

template<class U> auto_ptr<T>& operator=(auto_ptr<U>& rhs);

我发现 std::auto_ptr 有两个 operator=。我测试手动将其添加到界面,而编译器提示:“'auto_ptr'不能转换为'auto_ptr &'”。

这就是钥匙!!!那么我需要找到为什么!


当用户没有为类类型定义 operator= 时,编译器将生成一个。并与其他operator=比较,选择一个更特别的!

解决了!想想你所有的答案!谢谢你们的评论!

4

1 回答 1

1

我不确定我是否正确理解了您的问题,但是您的界面实际上没有定义复制赋值运算符(也没有定义复制构造函数),因为“模板复制 op=”不是真正的复制 op=(并且“模板复制 ctor" 不是真正的复制 ctor)。

这是一个显示问题的简单示例:

#include <cstdio>
using std::puts;

struct M {
    M& operator=(M const&) {
        puts("M::operator=(M const&)");
        return *this;
    }
};

template<typename T> class Foo {
    M m;
    template<typename U> friend class Foo; // (needed for m = rhs.m; below)
public:
    template<typename U> Foo& operator=(Foo<U> const& rhs) {
        puts("[template] Foo<T>::operator=(Foo<U> const&)");
        m = rhs.m; // calls M's op=
        return *this;
    }
};

int main() {
    puts("===");
    Foo<int> a;
    Foo<double> b;
    a = b;
    puts("---");
    Foo<int> c;
    Foo<int> d;
    c = d;
    puts("===");
}

打印:

===
[template] Foo<T>::operator=(Foo<U> const&)
M::operator=(M const&)
---
M::operator=(M const&)
===

为什么第二个作业只有一行?那是因为c = d;调用Foo<int>::operator=(Foo<int> const&)ie 真正的复制赋值运算符,并且由于我们没有声明它(仅是模板版本),编译器会自动生成它(它执行成员分配,因此调用M::operator=)。

因此,我必须将它显式添加到类中:

    // ...
    Foo& operator=(Foo const& other) {
        puts("[non-template] Foo<T>::operator=(Foo<T> const&)");
        m = other.m;
        return *this;
    }
};

然后打印:

===
[template] Foo<T>::operator=(Foo<U> const&)
M::operator=(M const&)
---
[non-template] Foo<T>::operator=(Foo<T> const&)
M::operator=(M const&)
===

因此,在您的示例中,p = foo();不会调用您的用户定义,template<class U> auto_ptr<T>& operator=(auto_ptr<U>& rhs)而是调用仅分配成员的隐式生成的版本pointee(不释放源)。

于 2013-09-07T17:32:02.377 回答