weak_ptr 使用 (自從C++11)
weak_ptr 設計的目的是爲配合 shared_ptr 而引入的一種智能指針來協助 shared_ptr 工作, 它只可以從一個 shared_ptr 或另一個 weak_ptr 對象構造, 它的構造和析構不會引起引用記數的增加或減少. 同時weak_ptr 沒有重載*和->但可以使用 lock 獲得一個可用的 shared_ptr 對象。
測試代碼:
#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
using namespace std;
class girl;
class boy
{
public:
boy()
{
cout << "boy construct!" << endl;
}
~boy()
{
cout << "boy destruct!" << endl;
}
void set_girl_friend(shared_ptr<girl>& g)
{
this->girl_friend = g;
}
private:
shared_ptr<girl> girl_friend;
};
class girl
{
public:
girl()
{
cout << "girl construct !" << endl;
}
~girl()
{
cout << "girl destruct!" << endl;
}
void set_boy_friend(shared_ptr<boy>& b)
{
this->boy_friend = b;
}
private:
shared_ptr<boy> boy_friend;
};
void use_trap()
{
shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
shared_ptr<boy> sp_boy(new boy()); /* 許仙 */
sp_girl->set_boy_friend(sp_boy);
sp_boy->set_girl_friend(sp_girl);
}
int main()
{
use_trap();
system("pause");
return 0;
}
執行:
修改測試代碼:
#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
using namespace std;
class girl;
class boy
{
public:
boy()
{
cout << "boy construct!" << endl;
}
~boy()
{
cout << "boy destruct!" << endl;
}
void set_girl_friend(shared_ptr<girl>& g)
{
this->girl_friend = g;
}
private:
shared_ptr<girl> girl_friend;
};
class girl
{
public:
girl()
{
cout << "girl construct !" << endl;
}
~girl()
{
cout << "girl destruct!" << endl;
}
void set_boy_friend(shared_ptr<boy>& b)
{
this->boy_friend = b;
}
private:
shared_ptr<boy> boy_friend;
};
void use_trap()
{
shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
shared_ptr<boy> sp_boy(new boy()); /* 許仙 */
sp_girl->set_boy_friend(sp_boy);
//sp_boy->set_girl_friend(sp_girl);
}
int main()
{
use_trap();
system("pause");
return 0;
}
畫圖理解:
老師的講解:
執行修改的測試代碼
demo
#include <stdio.h>
#include <iostream>
#include <string>
#include <memory>
#include <vector>
using namespace std;
class girl;
class boy
{
public:
boy()
{
cout << "boy construct!" << endl;
}
~boy()
{
cout << "boy destruct!" << endl;
}
void set_girl_friend(shared_ptr<girl>& g)
{
this->girl_friend = g;
}
private:
shared_ptr<girl> girl_friend;
};
class girl
{
public:
girl()
{
cout << "girl construct !" << endl;
}
~girl()
{
cout << "girl destruct!" << endl;
}
void set_boy_friend(shared_ptr<boy>& b)
{
this->boy_friend = b;
}
private:
shared_ptr<boy> boy_friend;
};
void use_trap()
{
shared_ptr<girl> sp_girl(new girl()); /* 白娘子 */
shared_ptr<boy> sp_boy(new boy()); /* 許仙 */
/* 弱指針的使用 */
weak_ptr<girl> wp_gir1; /* 定義空的弱指針 */
weak_ptr<girl> wp_gir2(sp_girl); /* 使用共享構造 */
wp_gir1 = sp_girl; /* 允許共享指針賦值給弱指針 */
cout << "sp_girl ref_count: " << sp_girl.use_count() << endl;
cout << "wp_girl.use_count: " << wp_gir1.use_count() << endl;
//(*sp_girl).set_boy_friend(sp_boy); 弱指針不支持* 和 -> 對指針訪問
//sp_girl->set_boy_friend(sp_boy);
// 在必要的時候可以轉成共享指針
shared_ptr<girl> sp_girl1;
sp_girl1 = wp_gir1.lock();
cout << " after lock wp_girl.use_count: " << wp_gir1.use_count() << endl;
sp_girl->set_boy_friend(sp_boy);
//sp_boy->set_girl_friend(sp_girl);
}
int main()
{
use_trap();
system("pause");
return 0;
}
執行:
結語:
弱指針主要是爲了解決, 交叉引用
時間: 2020-07-04