c – 如何实现std :: auto_ptr的复制构造函数?

前端之家收集整理的这篇文章主要介绍了c – 如何实现std :: auto_ptr的复制构造函数?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
回到我疯狂的 AutoArray thingy ……(引用那里的重要部分:
class AutoArray
{
    void * buffer;
public:
    //Creates a new empty AutoArray
    AutoArray();
    //std::auto_ptr copy semantics
    AutoArray(AutoArray&); //Note it can't be const because the "other" reference
                           //is null'd on copy...
    AutoArray& operator=(AutoArray);
    ~AutoArray();
    //Nothrow swap
    // Note: At the moment this method is not thread safe.
    void Swap(AutoArray&);
};

)

无论如何,试图实现复制构造函数.有一段客户端代码(尚未提交到bitbucket,因为它不会构建),如下所示:

AutoArray NtQuerySystemInformation(...) { ... };

AutoArray systemInfoBuffer = NtQuerySystemInformation(...);

这失败是因为复制构造函数将非const引用作为参数….但是我没有看到如何修改复制构造函数获取const引用,前提是修改了赋值中使用的源AutoArray(因此不会是const).你当然不能修改使用pass by value的东西,因为它是复制构造函数,并且它是一个无限循环!

如果我使用auto_ptr,这将是有效的:

std::auto_ptr NtQuerySystemInformation(...) { ... };

std::auto_ptr systemInfoBuffer = NtQuerySystemInformation(...);

那么,具有auto_ptr复制语义的类是否可以实现呢?

解决方法

auto_ptr使用脏技巧.

我将使用一个名为auto_int的dumbed-down类来演示复制构造功能,而不会带来模板或继承引入的任何复杂性.我认为代码大多是正确的,但它没有经过测试.我们的基本auto_int看起来像这样:

class auto_int
{
public:

    auto_int(int* p = 0) : p_(p) { }

    ~auto_int() { delete p_; }

    // copy constructor taking a non-const reference:
    auto_int(auto_int& other) 
        : p_(other.release()) { }

    int* release() 
    {
        int* temp = p_;
        p_ = 0;
        return temp;
    }

private:

    int* p_;
};

使用这个基本的auto_int,我们无法复制临时对象.我们的目标是能够写出如下内容

auto_int p(auto_int(new int()));

我们能做的是使用帮助类.对于auto_ptr,这称为auto_ptr_ref.我们称之为auto_int_ref:

class auto_int;

class auto_int_ref 
{
public:
    auto_int_ref(auto_int* p) : p_(p) { }

    auto_int& ref() { return *p_; }

private:
    auto_int* p_;
};

基本上,此类的实例只存储指向auto_int的指针,并允许我们将其用作auto_int的“引用”.

然后在我们的auto_int类中,我们需要两个额外的函数.我们需要另一个带有auto_int_ref的构造函数,我们需要一个转换运算符,它允许将auto_int隐式转换为auto_int_ref:

auto_int(auto_int_ref other)
    : p_(other.ref().release()) { }

operator auto_int_ref() { return this; }

这将允许我们“复制”一个临时的,同时仍然让复制构造函数采用非const引用.如果我们再看一下我们的示例代码

auto_int p(auto_int(new int()));

发生的是我们构造一个新的临时auto_int并将新的int()传递给带有int *的构造函数.然后使用运算符auto_int_ref()将此临时转换为指向它的auto_int_ref,并使用带有auto_int_ref的auto_int构造函数初始化p.

原文链接:https://www.f2er.com/c/116805.html

猜你在找的C&C++相关文章