Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
C++如何使用std::shared_from_this_C++智能指针管理与shared_from_this应用_创想鸟

C++如何使用std::shared_from_this_C++智能指针管理与shared_from_this应用

答案:std::enable_shared_from_this允许类通过shared_from_this安全返回指向自身的shared_ptr,前提是对象已被shared_ptr管理。

c++如何使用std::shared_from_this_c++智能指针管理与shared_from_this应用

在C++中,使用std::shared_ptr管理对象生命周期时,有时需要在类的成员函数内部返回当前对象的std::shared_ptr。直接通过newthis构造std::shared_ptr是危险且错误的做法,会导致多次释放或引用计数不一致。为了解决这个问题,C++提供了std::enable_shared_from_thisshared_from_this()机制。

什么是 shared_from_this?

std::enable_shared_from_this是一个模板基类,继承它后,派生类可以通过调用shared_from_this()安全地获取指向自身的std::shared_ptr。这个机制确保所有指向该对象的shared_ptr共享同一份引用计数。

关键点:只有当对象已经被至少一个std::shared_ptr管理时,才能调用shared_from_this(),否则会抛出std::bad_weak_ptr异常。

如何正确使用 enable_shared_from_this

以下是一个典型用法示例:

立即学习“C++免费学习笔记(深入)”;

#include #include class MyClass : public std::enable_shared_from_this {public:    MyClass() { std::cout << "MyClass constructedn"; }    ~MyClass() { std::cout << "MyClass destructedn"; }    // 安全返回当前对象的 shared_ptr    std::shared_ptr get_shared() {        return shared_from_this();    }    void do_something() {        std::cout << "Doing something...n";    }};int main() {    auto ptr = std::make_shared();  // 必须通过 shared_ptr 构造    auto another_ptr = ptr->get_shared();    // 正确:引用计数+1    ptr->do_something();    another_ptr->do_something();    return 0;}  // 两个 shared_ptr 离开作用域后,对象才被销毁

说明:

类必须继承std::enable_shared_from_this对象必须通过std::shared_ptr创建,不能是对象或裸指针直接构造调用shared_from_this()前,对象必须已被shared_ptr接管

常见错误与注意事项

以下几种情况会导致运行时错误或未定义行为:

在构造函数中调用shared_from_this():此时weak_ptr还未被初始化,会抛出异常。对栈上对象调用shared_from_this():即使类继承了enable_shared_from_this,但对象不是由shared_ptr管理,调用将失败。重复从同一个this创建shared_ptrstd::shared_ptr(this)多次调用会导致多个独立的引用计数,最终多次析构。

正确做法始终是:

使用std::make_shared()创建对象在需要返回自身共享指针的地方使用shared_from_this()避免在构造函数或析构函数中调用shared_from_this()

应用场景举例

这种机制常用于:

回调函数中传递自身:比如注册事件处理器时,需要把shared_ptr传给外部系统,防止对象提前销毁。实现工厂方法:在静态函数中创建对象并返回shared_ptr,同时允许内部方法获取共享所有权。父子对象关系管理:子对象需要持有父对象的shared_ptr时,可通过shared_from_this()安全获取。

基本上就这些。只要记住:继承enable_shared_from_this、用make_shared创建、不在构造/析构中调用shared_from_this,就能安全使用。

以上就是C++如何使用std::shared_from_this_C++智能指针管理与shared_from_this应用的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1484436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
c++如何使用std::async进行异步编程_c++异步任务与future用法说明
上一篇 2025年12月19日 08:05:27
C++中的完美转发(perfect forwarding)是什么_C++完美转发与std::forward用法解析
下一篇 2025年12月19日 08:05:35

相关推荐

发表回复

登录后才能评论
关注微信