2

我无法将智能指针的实例存储到容器中。这是指针的代码。

#include "std_lib_facilities.h"

template <class T>
class counted_ptr{
private:
    T* pointer;
    int* count;

public:
    counted_ptr(T* p = 0, int* c = new int(1)) : pointer(p), count(c) {}    // default constructor
    explicit counted_ptr(const counted_ptr& p) : pointer(p.pointer), count(p.count) { ++*count; } // copy constructor
    ~counted_ptr()
    {
        --*count;
        if(!*count) {
            delete pointer;
            delete count;
        }
    }

    counted_ptr& operator=(const counted_ptr& p)    // copy assignment
    {
        pointer = p.pointer;
        count = p.count;
        ++*count;
        return *this;
    }
    T* operator->() const{ return pointer; }
    T& operator*() const { return *pointer; }
    int& operator[](int index) { return pointer[index]; }

    int Get_count() const { return *count; }    // public accessor for count


};




int main()
{
    counted_ptr<double>one;
    counted_ptr<double>two(one);
    one = new double(5);
    vector<counted_ptr<double> >test;
}

在 int main() 中,该vector<counted_ptr<double> >行编译。当我第一次尝试它时vector<counted_ptr<double> >它没有编译(可能是因为它缺少参数。)但是,当我尝试使用 push_back 时,例如

test.push_back(one);

我得到一个编译器错误,它打开了 vector.tcc 并带有特定的错误说

no matching function for call to `counted_ptr<double>::counted_ptr(const counted_ptr<double>&)'|

我猜 push_back 找不到 counted_ptr,但我真的不确定。任何帮助表示赞赏,谢谢。

编辑:但是,这有效。测试[0] = 一;我猜 push_back 的语义是限制它的原因。

4

2 回答 2

5

你可能想试试这个:

test.push_back(counted_ptr<double>(one));

您的复制构造函数是显式的,这意味着编译器不会隐式调用它。

就个人而言,我会使原始指针构造函数显式,而复制 ctor 不显式。这将更接近通常的行为。

编辑:我还建议您实施交换方法。它使分配绝对微不足道。你最终会得到这样的结果:

counted_ptr &operator=(const counted_ptr &rhs) {
    counted_ptr(rhs).swap(*this);
    return *this;
}

这也具有在构造函数/析构函数中发生的所有会计的好处,这更易于管理:-)。

于 2009-08-22T18:09:55.910 回答
3

您的赋值运算符是错误的。
它指向的对象发生了什么?
如果您分配给自​​己或相同的内部对象会发生什么?

counted_ptr& operator=(const counted_ptr& p)    // copy assignment
{
    if (&p != this)
    {
        --*count;
        if ((*count) == 0) // Much more readable than !*count
        {
            delete pointer;
            delete count;
        }
        pointer = p.pointer;
        count = p.count;
        ++*count;
    }
    return *this;
}

注意:编写自己的智能指针不是一个好主意。它们并不像你想象的那么简单。

注意:这是我发现的第一件事。可能还有更多,我不确定这是否 100% 正确。

事实上,我会将赋值运算符更改为使用复制/交换 idium。

counted_ptr& operator=(const counted_ptr& p)    // copy assignment
{
    counted_ptr<T>     tmp(p);   // increment counter on p
    swap(tmp.pointer, pointer);
    swap(tmp.count    count);

    return *this;
                                 // tmp goes out of scope and thus the
                                 // destructor gets called and what was in this
                                 // object is now destroyed correctly.
}
// It may even be worth writing your own swap operator.
// Make sure you declare it as no-throw and grantee it.
于 2009-08-22T18:11:25.097 回答