0

When I try to construct an ostream from cout, I get an error error: call to deleted constructor of 'std::basic_ostream<char>'. Indeed, it is deleted in the ostream header file: basic_ostream(const basic_ostream&) = delete;. What is the rationale behind this deletion - why not allow programmers to copy an ostream?

(I ask also because I want to understand, more generally, what is the rationale behind deleting constructors).

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183
  • 1
    So, if you had a copy of a stream object and moved the file pointer in the copy, where would the file pointer in the original point? And what would happen to the copy if you closed the original? Or wrote something to the original - would that also write to the copy? – Adrian Mole Apr 24 '21 at 19:16
  • However, you *may* be able to open the same file object in two different streams: https://stackoverflow.com/q/59589175/10871073 – Adrian Mole Apr 24 '21 at 19:18
  • 1
    You can copy cout by passing its buffer. `std::ostream copy(std::cout.rdbuf())` – Offtkp Apr 24 '21 at 19:21
  • 1
    Note that streams are moveable, but not copyable. Ask yourself why streams have available move assignment / move constructors, but can't be copied. The reason is indicated by the duplicate link, in short, it doesn't make sense for a stream to be copied. – PaulMcKenzie Apr 24 '21 at 19:35

0 Answers0