Possible Duplicate:
What's the difference between cout<<cout and cout<<&cout in c++?
I accidentally found:
cout << cout;
The output is some address. What does this address mean, and why is it shown?
I am looking this question.
Thanks
Possible Duplicate:
What's the difference between cout<<cout and cout<<&cout in c++?
I accidentally found:
cout << cout;
The output is some address. What does this address mean, and why is it shown?
I am looking this question.
Thanks
Because ostream
overload operator void*()
, and that's the closes match for the call to operator <<
, the result of the cast (void*)cout
is printed. Which in your case is that address. Remember that cout
is an object.
Basically the call translates to:
cout.operator<<((void*)cout);
cout
is an ostream
object that have an overloaded insertion(<<
) operator. If we look at the constructor of the ostream
class, there is an argument to be passed which is a pointer to streambuf
object. streambuf
objects are normally associated with a character sequence which they use to read and write data. For a console application there will be such a character buffer associated with the standard output which might be used internally in case of cout. It is said in the documentation that we are not expected to directly instantiate the ostream
object but use any of the derived classes, ofstream
or ostringstream
diverting attention from stdout.
Regarding the address getting printed, I think Luchian Grigore is right.
This question will get you an idea on how and where the cout
object is instantiated: How is the object std::cout constructed/instantiated