对enable_shared_from_this有什么需求? [英] What is the need for enable_shared_from_this ?

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

问题描述

我是C ++ 11的新手,我遇到了enable_shared_from_this。我不明白它想要达到的目的是什么?所以我有一个使用enable_shared_from_this的程序。



I am new to C++11 and I came across enable_shared_from_this. I do not understand what it is trying to achieve? So I have a program that uses enable_shared_from_this.

struct TestCase: enable_shared_from_this<testcase>
{
    std::shared_ptr<testcase> getptr() {
        return shared_from_this();
    }
    
    ~TestCase() { std::cout << "TestCase::~TestCase()"; }
};


int main()
{
    std::shared_ptr<testcase> obj1(new TestCase);
    std::shared_ptr<testcase> obj2 = obj1->getptr();
   // The above can be re written as below, then what is the need for shared_from_this()
  //  std::shared_ptr<testcase> obj2 = shared_ptr<testcase>(obj1);
}

我的问题是当我需要一个指针' this ',为什么不使用obj本身。为什么要从该类的函数返回' this ',例如使用 getptr()然后返回 shared_from_this() ????

我不明白。



第二个问题,如果不使用 enable_shared_from_this ,为什么dtor被调用两次会造成问题,一次崩溃!!!!



我可以绕过 enable_shared_from_this 的另一种方式是这样的。

在TestCase类中添加这个



My question is when I need a pointer to 'this', why not use the obj itself. Why to return a 'this' from a function of that class like using getptr() and then returning shared_from_this()????
I do not understand.

Second question, if enable_shared_from_this is NOT used, why is the dtor called twice that creates a problem, a crash!!!!

Another way I can bypass using enable_shared_from_this is like this.
Add this in class TestCase

std::shared_ptr<testcase> getptr1(shared_ptr<testcase> obj) {
        return std::shared_ptr<testcase>(obj);
    }





从主要打电话这个:





and from main make a call this this:

std::shared_ptr<testcase> bp2 = bp1->getptr1(bp1);





完成。我们不需要 enable_shared_from_this 。为什么在地球上我们需要它?



我尝试过:



我试过谷歌,但我能找到的是什么是enable_shared_from_this,为什么使用它。但我的问题是没有其他替代品来启用enable_shared_from_this(正如我在我的问题中提到的那样)。



And done. We do not need enable_shared_from_this. Why on the earth do we need it??

What I have tried:

I have tried to google but what all I could find is what is enable_shared_from_this, why it is used. But my question is is there no other alternative to enable_shared_from_this (as I have mentioned in my question.)

推荐答案

使用谷歌,一个人可以轻松找到答案和样本之类的这一个: std :: enable_shared_from_this - cppreference.com [ ^ ]。



如果您发现难以理解文档,请在调试器中尝试示例。



那些链接也有助于理解目的:

enable_shared_from_this Class [ ^ ]。

安全地使用enable_shared_from_this | Musing Mortoray [ ^ ]

enable_shared_from_this - C ++ Reference [ ^ ]

c ++ - enable_shared_from_this的用处是什么 - Stack Overflow [ ^ ] enable_shared_from_this - 1.60.0 [ ^ ]



好​​吧,大多数情况下你可以通过写作来避免这个问题不同的代码。使用enable_shared_from_this本质上允许代码在其他情况下无法正常工作。通过使用它,它允许查找用于维护引用计数的内部数据。



据我所知,它允许成员函数调用使用shared_pointers的其他函数参数。



人们可以手工编写相似的代码...



更新:

以下是解释问题的代码...

Using Google, one ca easily find the answer and samples like this one: std::enable_shared_from_this - cppreference.com[^].

If you find hard to understand the documentation, then try the samples in a debugger.

Those links are also useful to understand the purpose:
enable_shared_from_this Class[^].
Safely using enable_shared_from_this | Musing Mortoray[^]
enable_shared_from_this - C++ Reference[^]
c++ - what is the usefulness of enable_shared_from_this - Stack Overflow[^]enable_shared_from_this - 1.60.0[^]

Well, in most cases you can avoid the problem by writing different code. Using enable_shared_from_this essentially allows the code to works properly in cases it would not have otherwise. By using that, it allows to find internal data used to maintain reference count.

As far as I understand, it allows member function to call other functions that uses shared_pointers as parameters.

One could probably write similar code by hand...

Update:
Here is code to explain the problem...
void f(shared_pointer<A> x) { }

class A 
{
public:
  A() { }
  ~A() { std::cout << "~A" << std::endl; }
  void test() { /* How do you call f function here? */ }

private:
  A(const A&);
  A& operator=(const A&);
};

int main()
{
  shared_pointer<A> a(new A);
  a->test();
}





使上面的代码与您一起使用简单的解决方案...如果您只需修改 test 函数并且没有析构函数被调用两次,那么在那一点上,想知道为什么代码没有复杂可能是合理的...



否则,通过使用 enable_shared_from_this ,您可以使代码按预期工作。



这本质上会在类中嵌入一个弱共享指针,这样你就可以从中创建多个共享指针(也就是说,可以从成员函数中访问的东西) 测试以上。



Make the code above work with you simple solution... If you can do it by only modifying test function and without the destructor being called twice, then at that point, it might be legitimate to wonder why the code is no complicated...

Otherwise, by using enable_shared_from_this, you can make that code works as expected.

This would essentially embed a weak shared pointer in the class so that you can create multiple shared pointers from this (that is, Something that is accessible from a member function like test above.


这篇关于对enable_shared_from_this有什么需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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