std :: shared_ptr这个 [英] std::shared_ptr of this

查看:106
本文介绍了std :: shared_ptr这个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试学习如何使用智能指针。但是,在做一些实验时,我发现了以下情况,我找不到一个饱和的解决方案:



想象你有一个对象的类A是类的对象的父类B(孩子),但两者应该彼此认识:

  A类; 
class B;

class A
{
public:
void addChild(std :: shared_ptr< B> child)
{
children-> push_back(child);

//如何正确传递指针?
// child-> setParent(this); // wrong
// ^^^^
}

private:
std :: list< std :: shared_ptr< B>儿童;
};

class B
{
public:
setParent(std :: shared_ptr< A> parent)
{
this-> parent = parent;
};

private:
std :: shared_ptr< A>父母
};问题是A类的对象如何传递一个 std ::



有Boost的解决方案( this 共享指针(获取 boost :: shared_ptr this ),但是如何使用 std :: 智能指针处理这个问题? / p>

解决方案

std :: enable_shared_from_this 仅用于此目的。您可以继承它,并且可以调用 .shared_from_this() 。此外,您在此处创建循环依赖性,这可能导致资源泄漏。这可以使用 std :: weak_ptr 。所以你的代码可能看起来像这样(假设孩子依赖父的存在,而不是相反):

  class A; 
class B;

class A
:public std :: enable_shared_from_this< A>
{
public:
void addChild(std :: shared_ptr< B> child)
{
children.push_back(child);

//像这样
child-> setParent(shared_from_this()); // ok
// ^^^^^^^^^^^^^^^^^
}

private:
// note weak_ptr
std :: list< std :: weak_ptr< B>>儿童;
// ^^^^^^^^
};

class B
{
public:
void setParent(std :: shared_ptr< A> parent)
{
this-> parent = parent;
}

private:
std :: shared_ptr< A>父母
};

但是,调用 .shared_from_this()要求在呼叫时由 std :: shared_ptr 拥有。这意味着你不能在堆栈上创建这样的对象,并且不能在构造函数或析构函数中调用 .shared_from_this()


I am currently trying to learn how to use smart pointers. However while doing some experiments I discovered the following situation for which I could not find a satifying solution:

Imagine you have an object of class A being parent of an object of class B (the child), but both should know each other:

class A;
class B;

class A
{
public:
    void addChild(std::shared_ptr<B> child)
    {
        children->push_back(child);

        // How to do pass the pointer correctly?
        // child->setParent(this);  // wrong
        //                  ^^^^
    }

private:        
    std::list<std::shared_ptr<B>> children;
};

class B
{
public:
    setParent(std::shared_ptr<A> parent)
    {
        this->parent = parent;
    };

private:
    std::shared_ptr<A> parent;
};

The question is how can an object of class A pass a std::shared_ptr of itself (this) to its child?

There are solutions for Boost shared pointers (Getting a boost::shared_ptr for this), but how to handle this using the std:: smart pointers?

解决方案

There is std::enable_shared_from_this just for this purpose. You inherit from it and you can call .shared_from_this() from inside the class. Also you are creating circular dependencies here that can lead to resource leaks. That can be resolved with the use of std::weak_ptr. So your code might look like this (assuming children rely on existence of parent and not the other way around):

class A;
class B;

class A
    : public std::enable_shared_from_this<A>
{
public:
    void addChild(std::shared_ptr<B> child)
    {
        children.push_back(child);

        // like this
        child->setParent(shared_from_this());  // ok
        //               ^^^^^^^^^^^^^^^^^^
    }

private:     
    // note weak_ptr   
    std::list<std::weak_ptr<B>> children;
    //             ^^^^^^^^
};

class B
{
public:
    void setParent(std::shared_ptr<A> parent)
    {
        this->parent = parent;
    }

private:
    std::shared_ptr<A> parent;
};

Note however, that calling .shared_from_this() requires that this is owned by std::shared_ptr at the point of call. This means that you cannot create such object on stack anymore, and cannot call .shared_from_this() from within a constructor or destructor.

这篇关于std :: shared_ptr这个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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