public member function
<ostream> <iostream>

std::basic_ostream::basic_ostream

initialization (1)
explicit basic_ostream (basic_streambuf<char_type,traits_type>* sb);
initialization (1)
explicit basic_ostream (basic_streambuf<char_type,traits_type>* sb);
copy (2)
basic_ostream& (const basic_ostream&) = delete;
move (3)
protected: basic_ostream& (basic_ostream&& x);
Construct object
Constructs a basic_ostream object.

(1) inititalization constructor
Assigns initial values to the components of its base classes by calling the inherited member basic_ios::init with sb as argument.
(2) copy constructor (deleted)
Deleted: no copy constructor.
(3) move constructor (protected)
Acquires the contents of x, except its associated stream buffer: It calls basic_ios::move to transfer x's internal components inherited from basic_ios. x is left with its associated stream buffer unchanged and not tied (all other components of x are in an unspecified but valid state after the call).

Parameters

sb
pointer to a basic_streambuf object with the same template parameters as the basic_ostream object.
char_type and traits_type are member types defined as aliases of the first and second class template parameters, respectively (see basic_ostream types).
x
Another basic_ostream of the same type (with the same class template arguments charT and traits).

Example

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;
}

This code uses a filebuf object (derived from streambuf) to open the file test.txt. The buffer is then passed as parameter to the ostream constructor, associating it to the stream.

Objects of class ostream are seldom constructed directly. Generally some derived class is used (like the standard basic_ofstream and basic_ostringstream).

Data races

The object pointed by sb may be accessed and/or modified.

Exception safety

If an exception is thrown, the only side effects may come from accessing/modifying sb.

See also