THE2ndMOUSE
THE2ndMOUSE

Reputation: 52

Difference between 'cout<<cout' and 'cout<<&cout' in c++?

I want to know actual difference between cout<<cout and cout<<&cout in c++? In my compiler cout<<cout returns 0x477864 & cout<<&cout returns 0x477860 at any time.It shows it has 1 digit of difference between them.What are the significance of these?

Upvotes: 1

Views: 118

Answers (1)

Benjamin Lindley
Benjamin Lindley

Reputation: 103713

When you do this:

cout << cout;

You are relying on the stream's implicit conversion to void*. This value is used (pre-c++11) for testing the state of the stream. It is unspecified what the value actually is, it just needs to be NULL if the stream is in a fail state, and a non NULL otherwise. Maybe it's returning the address of a member of the object, but it's not really important, and is implementation defined.

When you do this:

cout << &cout;

That is getting the actual address of cout.

Note that in C++11 and beyond, the first one, cout << cout;, will no longer compile, because the implicit conversion to void* no longer exists. Instead there is an explicit conversion to bool to serve the same purpose.

Upvotes: 8

Related Questions