从enable_shared_from_this继承的目的是什么?

时间:2017-11-14 12:05:11

标签: c++11

class Tstd::enable_shared_from_this<T>继承的重点是什么?我似乎无法弄清楚为什么你不会只创建一个std::shared_ptr<this>

1 个答案:

答案 0 :(得分:1)

Cppreference has a good example on why

如果您想std::shared_ptr this *this,而std::shared_ptr已归shared_from_this()所有,您不会返回std::shared_ptr<T>(this)但返回新的use_count()然后你会得到2个共享指针,它们不知道它们都拥有相同的对象,因此delete会出错,这将导致双tt_content.highlight_list.20.text.20.parseFunc.externalBlocks = table,blockquote,ol,ul,li tt_content.highlight_list.20.text.20.parseFunc.externalBlocks { li.stripNL = 1 li.callRecursive = 1 li.callRecursive.tagStdWrap.HTMLparser = 1 li.callRecursive.tagStdWrap.HTMLparser.tags.li { fixAttrib.class.default = col-4 col-sm-2 col-md-2 } ul.stripNL = 1 ul.callRecursive = 1 ul.callRecursive.tagStdWrap.HTMLparser = 1 ul.callRecursive.tagStdWrap.HTMLparser.tags.ul { fixAttrib.class.default = row } } ,这是未定义的行为。

相关问题