我使用了這么段程式
#include <memory>
#include <iostream>
#include <list>
using namespace std;
void fun2();
class Test
{
public:
~Test()
{
fun2();
}
};
std::list<weak_ptr<Test>> lst;
void fun2()
{
auto iter = lst.begin();
while (iter != lst.end())
{
if (iter->expired())
iter = lst.erase(iter);
else
++iter;
}
}
void fun()
{
shared_ptr<Test> s(make_shared<Test>());
lst.push_back(s);
}
int main()
{
fun();
}
該程式在智能指標銷毀時時 檢測weak_ptr里的內容是否被洗掉了,如果內容被洗掉了那么在串列里洗掉weak_ptr
我現在想問下,在解構式中這么做expired是否合法,有沒有明確定義這個行為(執行shared_ptr管理的物件的解構式時,weak_ptr的expired為true)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240679.html
標籤:C++ 語言
上一篇:荷蘭國旗問題
