引用指针后面的值
reference to a value behind a pointer
我想要对指针后面的值的引用。
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();
您正在尝试复制std::fstream
,这是不允许的。
错误不在您的类中,而是在呼叫站点。 auto fs = ...
不创建引用,但尝试调用复制构造函数;auto
只能代替std::fstream
,不能代替&
。
试试这个:
auto& fs = concrete_call->GetStreamAttachementDestination_AsReference();
相关文章:
- 如何使用基类指针引用派生类成员
- 将常量指针引用绑定到非常量指针
- 如果非动态变量被指针引用,何时超出范围?
- 转换指针引用的字符串
- 指针引用的生存期(以 C++为单位)
- 从 unique_ptr::get 发送指针作为指针引用进入函数
- 指针/引用的 CLion 格式
- 使用模板专用化来比较指针引用
- 如何保存指向抽象基类的指针/引用,但在 c++ 中仍然可以复制
- C++初始化指针/引用/复制细微差别
- 用数组或指针引用函数?
- 指针范围问题和返回类中封装的指针向量内的指针引用
- 强制转换为指针引用是否会导致未定义的行为
- 访问由 void 指针引用的结构的成员
- C 为什么当先前的步骤引发异常时,std :: shared_ptr的指针引用会被破坏
- 动态指针引用数组由三元运算符返回值,但有异常
- 悬空指向 int 和 char* 常量的指针/引用
- C++ 二维数组和指针引用
- 通过使用指针/引用,在C++中使用向量加快计算速度
- C++非类型模板模板到函数指针/引用