8.1.1. No Copy or Assign for IO Objects

Image

As we saw in § 7.1.3 (p. 261), we cannot copy or assign objects of the IO types:

ofstream out1, out2;out1 = out2;              // error: cannot assign stream objectsofstream print(ofstream); // error: can't initialize the ofstream parameterout2 = print(out2);       // error: cannot copy stream objects

Because we can’t copy the IO types, we cannot have a parameter or return type that is one of the stream types (§ 6.2.1, p. 209). Functions that do IO typically pass and return the stream through references. Reading or writing an IO object changes its state, so the reference must not be const.

Get C++ Primer, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.