2017-06-29 131 views
0

在我正在寫的程序中,我有一個創建和處理某些線程的類。構造完成後,這個實例將被賦予另一個類的對象,線程將能夠調用其成員函數。通過智能指針調用另一個類的成員函數

我已經得到了這個與原指針(只需更換智能指針),但由於我有權訪問智能指針,我試圖用它們來代替。雖然沒有太多進展。

一些網站的搜索導致我使用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,但我不明白爲什麼。由於objshared_ptr,因此不應該調用shared_from_this()有效嗎?

編譯g++ main.cppgcc 7.1.1

+0

隨意重新翻譯標題問題。還有很多其他問題相匹配,但我不知道還有什麼可以稱之爲的。 – Tmplt

回答

2

shared_from_this只適用於之後你被包裝在一個共享的指針

在構建點上,還沒有共享指針。所以你不能shared_from_this直到你的構造函數完成後。

解決這個問題的方法是舊的「虛擬構造函數」技巧。

class Obj : std::enable_shared_from_this<Obj> { 
    struct token {private: token(int){} friend class Obj;}; 
public: 
    static std::shared_ptr<Obj> create(Caller& c); 
    Obj(token) {} 
}; 

inline std::shared_ptr<Obj> Obj::create(Caller& c) { 
    auto r = std::make_shared<Obj>(token{0}); 
    c.set_obj(r); 
    return r; 
} 

然後在測試代碼:

Caller c; 
auto obj = Obj::create(c); 

c.o->dothing(); 

live example


虛構造既不虛擬也不構造函數。

+0

謝謝,這個工程。但你的意思是讓構造函數是私有的嗎? 'g ++'不會編譯它,傾倒很多以'[constructor]在這裏聲明爲私有結尾的錯誤' – Tmplt

+0

@tmp fixed,now with token security。 – Yakk

+0

它被稱爲工廠模式 – rustyx

相關問題