2017-03-18 55 views
0

我想引用指針後面的值。引用指針後面的值

class UnicastCall { 
protected: 
    std::fstream *m_stream_attachement_destination_; 
... 
public: 
auto GetStreamAttachementDestination_AsPointer() -> decltype(m_stream_attachement_destination_) 
    { return m_stream_attachement_destination_; } //THIS WORKS 
auto GetStreamAttachementDestination_AsReference() -> decltype(*m_stream_attachement_destination_) & 
    { return *m_stream_attachement_destination_; } //IS THIS CORRECT? 
.... 
}; 

但我得到一個錯誤。

error: use of deleted function 'std::basic_fstream<_CharT, _Traits>::basic_fstream(const std::basic_fstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits<char>]' 
auto fs = concrete_call->GetStreamAttachementDestination_AsReference(); 

回答

2

您試圖複製std::fstream,這是不允許的。

錯誤不在您的班級,但在呼叫站點auto fs = ...不會創建引用,但會嘗試調用複製構造函數; auto僅是std::fstream的替代品,而不是&

試試這個:

auto& fs = concrete_call->GetStreamAttachementDestination_AsReference(); 
+0

太棒了!編譯和工作 –