public member function
<istream> <iostream>

std::istream::istream

初始化 (1)
explicit istream (streambuf* sb);
初始化 (1)
explicit istream (streambuf* sb);
复制 (2)
istream& (const istream&) = delete;
移动 (3)
protected: istream& (istream&& x);
构造对象
构造一个 istream 对象。

(1) 初始化构造函数
通过调用继承的成员 ios::init 并将 sb 作为参数,为基类的组成部分分配初始值。
(2) 复制构造函数 (已删除)
已删除:没有复制构造函数。
(3) 移动构造函数 (受保护)
获取 x 的内容,但不包括其关联的 流缓冲区:它复制 xgcount 值,然后调用 ios::move 来转移 xios 组成部分。xgcount 值将变为零,未被 *绑定*,并且其关联的 流缓冲区 保持不变(调用后 x 的所有其他组成部分处于未指定但有效状态)。

参数

sb
streambuf 对象的指针。
x
另一个 istream 对象。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// istream constructor
#include <iostream>     // std::ios, std::istream, std::cout
#include <fstream>      // std::filebuf

int main () {
  std::filebuf fb;
  if (fb.open ("test.txt",std::ios::in))
  {
    std::istream is(&fb);
    while (is)
      std::cout << char(is.get());
    fb.close();
  }
  return 0;
}

此示例代码使用一个 filebuf 对象(从 streambuf 派生)来打开一个名为 test.txt 的文件。该缓冲区作为参数传递给 istream 对象 is 的构造函数,将其与流关联。然后,程序使用输入流将其内容打印到 cout

istream 类的对象很少被直接构造。通常使用派生类(如标准的 ifstreamistringstream)。

数据竞争

sb 指向的对象可能被访问和/或修改。

异常安全

如果抛出异常,唯一的副作用可能来自对 sb 的访问/修改。

另见