Search code examples
c++iostream

‘std::ostream’ has no member named ‘close’


std::ostream has no member function close(). What type of stream should I not be allowed to close?

As an example, maybe I would like to close std::cout in order to prevent any further writing to it.

std::cout.close(); // ‘std::ostream’ has no member named ‘close’

If I were using the C library, I could close stdout with:

fclose(stdout); // no problem

So what is the idea behind leaving out the close() member from std::ostream?


Related:


Solution

  • It does not make sense to have the close() function as a member of std::ostream. The first example would be that std::ostringstream inherits from std::ostream. Would it make sense to close a string? The only members of std::ostream are the global objects for input/output.

    The filestreams have a close() function because it is important to be able to release the resource to the environment. However, since there are other classes that inherit from this base class that don't need this function, it wouldn't make sense for it to be a part of std::ostream and that's why it is only used in the filestreams.