在我正在寫的程序中,我有一個創建和處理某些線程的類。構造完成後,這個實例將被賦予另一個類的對象,線程將能夠調用其成員函數。通過智能指針調用另一個類的成員函數
我已經得到了這個與原指針(只需更換智能指針),但由於我有權訪問智能指針,我試圖用它們來代替。雖然沒有太多進展。
一些網站的搜索導致我使用shared_ptr
S,所以這裏就是我想要做的事:
Obj.hpp
:
#pragma once
#include "Caller.hpp"
class Caller;
class Obj : std::enable_shared_from_this<Obj> {
public:
Obj(Caller &c);
void dothing();
};
Caller.hpp
:
#pragma once
#include <memory>
#include "Obj.hpp"
class Obj;
class Caller {
public:
void set_obj(std::shared_ptr<Obj> o);
std::shared_ptr<Obj> o;
};
main.cpp
:
#include <iostream>
#include <memory>
#include "Caller.hpp"
#include "Obj.hpp"
void Caller::set_obj(std::shared_ptr<Obj> o)
{
this->o = o;
}
Obj::Obj(Caller &c)
{
c.set_obj(shared_from_this());
}
void Obj::dothing()
{
std::cout << "Success!\n";
}
int main()
{
Caller c;
auto obj = std::make_shared<Obj>(c);
c.o->dothing();
}
運行此代碼導致拋出std::bad_weak_ptr
,但我不明白爲什麼。由於obj
是shared_ptr
,因此不應該調用shared_from_this()
有效嗎?
編譯g++ main.cpp
與gcc 7.1.1
。
隨意重新翻譯標題問題。還有很多其他問題相匹配,但我不知道還有什麼可以稱之爲的。 – Tmplt