However, this is a non-standard solution, and may decrease portability. If you do not want to deal with stream buffers at all, you can also use pointers or references to streams instead. A pointer to an ostream is used. A file stream for the named output file is created on the heap and assigned to the pointer, in case a file name is provided. Otherwise, a pointer to cout is used.
Output is written through the pointer to either cout or the named output file.
In summary, creating a copy of a stream is not trivial and should only be done if you really need a copy of a stream object. In many cases, it is more appropriate to use references or pointers to stream objects instead, or to share a stream buffer between two streams. Despite the previous caveats, there are situations where sharing a stream buffer among streams is useful and intended. Let us focus on these in this section. Imagine you need different formatting for different kinds of output to the same stream.