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

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

概述回到我疯狂的 AutoArray thingy ……(引用那里的重要部分: class AutoArray{ void * buffer;public: //Creates a new empty AutoArray AutoArray(); //std::auto_ptr copy semantics AutoArray(AutoArray&); // 回到我疯狂的 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.

总结

以上是内存溢出为你收集整理的c – 如何实现std :: auto_ptr的复制构造函数?全部内容,希望文章能够帮你解决c – 如何实现std :: auto_ptr的复制构造函数?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1236254.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存