public member function
<ostream> <iostream>

std::basic_ostream::basic_ostream

初始化 (1)
explicit basic_ostream (basic_streambuf<char_type,traits_type>* sb);
初始化 (1)
explicit basic_ostream (basic_streambuf<char_type,traits_type>* sb);
复制 (2)
basic_ostream& (const basic_ostream&) = delete;
移动 (3)
protected: basic_ostream& (basic_ostream&& x);
构造对象
构造一个 basic_ostream 对象。

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

参数

sb
指向 basic_streambuf 对象的指针,其模板参数与 basic_ostream 对象相同。
char_typetraits_type 是成员类型,分别定义为第一个和第二个类模板参数的别名(参见 basic_ostream 类型)。
x
另一个 basic_ostream 对象,类型相同(具有相同的类模板参数 charTtraits)。

示例

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

int main () {
  std::filebuf fb;
  fb.open ("test.txt",std::ios::out);
  std::ostream os(&fb);
  os << "Test sentence\n";
  fb.close();
  return 0;
}

此代码使用 filebuf 对象(从 streambuf 派生)打开文件 test.txt。然后将缓冲区作为参数传递给 ostream 构造函数,将其与流关联。

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

数据竞争

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

异常安全

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

另见