在不转移所有权的情况下返回unique_ptr私有成员数据 [英] Returning unique_ptr private member data without transferring ownership

查看:51
本文介绍了在不转移所有权的情况下返回unique_ptr私有成员数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文

以下错误似乎是在告诉我,我无法从此get函数返回名为 m_head unique_ptr .我只想返回我的 unique_ptr m_head 而不转移所有权.

The error below appears to be telling me that I can not return my unique_ptr called m_head from this get function. I just want to return my unique_ptr m_head without transferring ownership.

自从引入智能指针以来,我一直在完全避免使用原始指针,因为原始指针不是异常安全的,具有内存管理开销和我所知道的其他问题.也许在某些情况下,我应该在较小范围内简要使用它们?

I've been avoiding raw pointers completely since introduced to smart pointers since raw pointers are not exception safe, have the memory management overhead and other issues I've been aware of. Maybe there are cases like this where I should use them briefly contained in a small scope?

在这种情况下,我认为我需要转移所有权,而不是目前的方法.我应该改为由 unique_ptr 管理对象,创建一个新的 shared_ptr 来管理该对象,然后返回 shared_ptr ,但需要一些确认.我认为可能是因为 std :: unique_ptr 文档说:

In this I think instead of my current approach I need to transfer ownership. I should instead get the object managed by the unique_ptr, create a new shared_ptr to manage the object, then return the shared_ptr, but need some confirmation. I think this may be the case because the std::unique_ptr docs say:

unique_ptr对象唯一拥有其指针:不得使用其他设施负责删除对象,因此没有其他托管指针应该指向其托管对象,因为他们必须尽快unique_ptr对象删除其托管对象而无需考虑说明其他指针是否仍指向同一对象,因此将所有其他指向该指针的指针保留为无效的位置.

错误

 `function "std::unique_ptr<_Ty, _Dx>::unique_ptr(const std::unique_ptr<_Ty, _Dx> &) 
 [with _Ty=mrobsmart::LinkedList::Node, _Dx=std::default_delete<mrobsmart::LinkedList::Node>]" 

 (declared at line 2337 of "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.15.26726\include\memory")

 cannot be referenced  -- it is a deleted function

代码

#include <memory>

class LinkedList
{
    private:
        std::unique_ptr<Node> m_head;

    public:
        LinkedList(int data) {
            m_head = std::make_unique<Node>(data);
        }

        const std::unique_ptr<Node> get_head() { return m_head; }
};

推荐答案

我只想返回我的unique_ptr m_head而不转移所有权.

I just want to return my unique_ptr m_head without transferring ownership.

那是不可能的. unique_ptr 是围绕每一步转移其所有权的行为而设计的.

That's not possible. unique_ptr is designed around the behavior that every move transfers its ownership.

注意,自从引入智能指针以来,我一直在完全避免使用原始指针,因为原始指针不是异常安全的,具有内存管理开销和我所知道的其他问题,但是也许在某些情况下,应该在小范围内简短使用它们吗?

Note, I've been avoiding raw pointers completely since introduced to smart pointers since raw pointers are not exception safe, have the memory management overhead and other issues I've been aware of, but maybe there are cases like this where I should use them briefly contained in a small scope?

原始指针不是邪恶的.将它们用作纯引用/间接引用是一个完全有效的用例-不涉及所有权,内存管理或异常安全性.

Raw pointers are not evil. Using them as pure references/indirections is a perfectly valid use case -- there is no ownership, memory management or exception safety involved.

当然,也可以返回C ++引用.选择指针还是引用取决于该值是否可以为null,但最终还是代码风格的问题.

Of course, it's also possible to return a C++ reference. Whether you choose a pointer or a reference can depend on whether the value can be null, but ultimately is also a question of code style.

因此,其中任何一个(重要: const -限定该功能):

So, either of those (important: const-qualify the function):

    const Node* get_head() const { return m_head.get(); }
    const Node& get_head() const { return *m_head; }

这篇关于在不转移所有权的情况下返回unique_ptr私有成员数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆