-2

I saw the code snippet below somewhere.

#include <iostream>


int main()
{
    std::ostream& os = std::cout;

    os << "thanks a lot" << std::endl;
    return 0;
}

Since the aforementioned code snippet works well, it indicates that std::cout is derived from std::ostream. But I can't find any direct reference yet.

As per the document, which says that[emphasis mine]:

The global objects std::cout and std::wcout control output to a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stdout.

The above quotation says that std::cout controls ouput to a type which derived from std::streambuf other than std::cout derived from std::streambuf.

And I only find the declaration of std::cout in a file named /usr/include/c++/7/iostream:

  extern ostream cout;      /// Linked to standard output

I can't find the implementation of std::cout.

John
  • 2,963
  • 11
  • 33
  • 2
    I'm not sure what you mean by "the document", it sounds like you mean [cppreference](https://en.cppreference.com/w/cpp/io/cout). Which specifies right there that the declaration of `std::cout` is `extern std::ostream cout;`, which means that `std::cout` doesn't just derive from `std::ostream`, it _is_ a `std::ostream` object. – Nathan Pierson Jun 20 '22 at 12:59
  • https://timsong-cpp.github.io/cppwp/input.output#lib:cout – NathanOliver Jun 20 '22 at 12:59
  • @NathanPierson Sorry, I forgot to paste the link. Updated now. – John Jun 20 '22 at 13:00
  • Also, in your link right at the top it has `extern std::ostream cout;`, so you know `cout` is an `ostream` object – NathanOliver Jun 20 '22 at 13:01
  • @NathanOliver Thank you for pointing out that. I have thought this question for about half an hour. I just realised that the type of `std::cout` is `std::ostream` after I just posted this post and look the declaration again. – John Jun 20 '22 at 13:03

3 Answers3

5

ostream is a class. cout is an instance of that class.

This is no different from class Person {}; Person john;. Person is the class, john is an instance of that class. The C++ standard library just happens to create an instance (cout) of this particular class (ostream) ahead of time, configured to write to the standard output stream.

The line std::ostream& os = std::cout; defines a new variable called os, which is of type ostream&, that is a reference to an ostream. It then makes it a reference to the already defined variable cout.

user229044
  • 232,980
  • 40
  • 330
  • 338
2

Since the aforementioned code snippet works well, it indicates that std::cout is derived from std::ostream.

Not quite. The code works because std::cout is a std::ostream. No inheritance needed to read and understand that code example.

The above quotation says that std::cout controls ouput to a type which derived from std::streambuf other than std::cout derived from std::streambuf.

The quote is talking about details that you need not care about (unless you do care about them ;). The important part of extern ostream cout; for this quesiton is ostream cout; which means cout is an instance of type ostream (and extern just indicates that it is only a declaration while the definition (of the instance) is elsewhere, When to use extern in C++).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
0

To help the readers of this post, see the code snippet below

#include<iostream>
#include<typeinfo>

int main()
{
    std::cout << typeid(std::cout).name() << std::endl;
    std::cout << typeid(std::ostream).name() << std::endl;

    return 0;
}

Here is the output:

So
So

The output indicates that they are the same type.

John
  • 2,963
  • 11
  • 33