使用std::istream引用初始化初始化初始化

Class with std::istream reference initialization

本文关键字:初始化 std 引用 istream 使用      更新时间:2023-10-16

初始化这个类时遇到问题:

class Player{
  ///
  std::istream ∈
  ///
};

这样尝试:

Player::Player():in(cin){
  ///
}

有人能为我指明如何实现这一目标的正确方向吗?此外,在初始化之后,我可以通过说之类的话来更改引用吗

stringstream ss("test");
Player p;
p.in = ss;

提前感谢

您没有声明构造函数,只定义了它。
声明构造函数并将其设置为公共:

class Player{
public:
  Player(); // You need to declare the constructor
  std::istream ∈
};
Player::Player():in(cin)
{}
int main()
{
    Player p;
}

我能更改参考号吗?

不,您不能更改引用,只能将值更改为正在引用的值。