2017-03-18 27 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

太棒了!编译和工作 –