简体   繁体   English

在小部件之间传输std :: unique_ptr

[英]Transferring std::unique_ptr between Widgets

I have a Widget class, which is supposed to have unique ownership of another Widget. 我有一个Widget类,应该具有另一个Widget的唯一所有权。 I want to implement a function which passes this Widget to another Widget. 我想实现一个将此小部件传递给另一个小部件的功能。 I learnt that the best way to implement the concept of single ownership is std::unique_ptr , however I can't get it to run 我了解到,实现单一所有权概念的最佳方法是std::unique_ptr ,但是我无法使其运行

This is the code: 这是代码:

class Widget
{
public:
    Widget(void) { }
    virtual ~Widget(void) { /* */ }
    void successor(Widget&& successor)
    {
        this->_successor = std::unique_ptr<Widget>(&successor);
    }
    void refer_value(int a)
    {
        _successor->value = a;  
    }
    auto refer_value(void) const -> int
    {
        return _successor->value;
    }
    auto get(void) -> Widget&
    {
        return *_successor;
    }

    int value;
private:
    std::unique_ptr<Widget> _successor;
};

It compiles without a problem, but when I try to run this: 编译没有问题,但是当我尝试运行此命令时:

int main(void)
{
    Widget a{};
    Widget b{};
    Widget c{};

    a.successor(Widget{});
    a.refer_value(5);

    b.successor(std::move(a.get()));
    std::cout << b.refer_value();
}

I get a Segmentation Fault. 我遇到了细分错误。 What is the problem? 问题是什么?

And additionally, How would I have to write an implementation of the successor() function, if I wanted to pass Widget c . 另外,如果我想传递Widget c ,我将不得不如何编写一个successor()函数的实现。 I could only get it running for rvalues. 我只能让它为右值运行。

Here 这里

this->_successor = std::unique_ptr<Widget>(&successor);

You're creating a unique_ptr to an object that would be destroyed once the scope exits. 您正在为对象创建一个unique_ptr ,一旦范围退出,该对象将被销毁。

Also, here 也在这里

b.successor(std::move(a.get()));

You're taking the address of an existing pointer in a unique_ptr , and assigning it to yet another unique_ptr . 您要在unique_ptr现有指针的地址,并将其分配给另一个 unique_ptr It's basically wrong/double deallocations all over the place here. 在这里到处都是错误/双重释放。

I'm highly unaware of what you're trying to do here. 我不知道高度的你想在这里做什么。 At any rate, if I can just limit myself to the number of changes relevant to memory management here in order to have this code make sense: 无论如何,如果我可以将自己限制在与内存管理相关的更改数量上,以使此代码有意义:

class Widget
{
public:
    Widget(void) { }

    virtual ~Widget(void) { /* */ }

    void successor(std::shared_ptr<Widget> successor)
    {
        this->_successor = std::move(successor);
    }

    void refer_value(int a)
    {
        _successor->value = a;
    }

    auto refer_value(void) const -> int
    {
        return _successor->value;
    }

    std::shared_ptr<Widget> get()
    {
        return _successor;
    }

    int value;

private:
    std::shared_ptr<Widget> _successor;
};

int main(void)
{
    Widget a{};
    Widget b{};
    Widget c{};

    a.successor(std::make_shared<Widget>());
    a.refer_value(5);

    b.successor(a.get());
    std::cout << b.refer_value();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM