shared_ptr循环引用例子

#include <bits/stdc++.h>
using namespace std;
class A{
public:
    shared_ptr<A> t;
    ~A(){
        cout<<1<<endl;
    };
};
int main(){
    shared_ptr<A> q(new A());
    shared_ptr<A> qq(new A());
    q->t=qq;
    qq->t=q;
    cout<<q.use_count()<<endl;
}

解决方法,类中的设置成weak_ptr。

发布了81 篇原创文章 · 获赞 94 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_41033366/article/details/105456816