what does cout return?In code
I am getting a hexadecimal number,probably an address.Does cout return something?Thanking in advanceCode:cout<<cout<<"Hello World";
what does cout return?In code
I am getting a hexadecimal number,probably an address.Does cout return something?Thanking in advanceCode:cout<<cout<<"Hello World";
cout doesn't return anything. The '<<' operator returns cout.
When you write (cout << "x" << 3 << " crazy!" << endl), evaluation happens like this:
Code:((cout << "x") << 3) << " crazy!") << endl ((cout << 3) << " crazy!") << endl (cout << " crazy!") << endl cout << endl cout
As written, I'd say you're outputting the address of a function.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Which function's address?.I havenot made any function yet.
In above code i have not made or called any function.So which function's address are you taking of.Code:#include<iostream.h> int main() { cout<<cout<<"Hello world"; return(0); }
>I havenot made any function yet.
main is a function, so this statement is false.
>what does cout return?
cout is an object, it doesn't return anything. The << operator returns a reference to the stream to which it's applied, in this case cout. So the address you're seeing is that of cout.
My best code is written with the delete key.
I thought that also but its not :)cout is an object, it doesn't return anything. The << operator returns a reference to the stream to which it's applied, in this case cout. So the address you're seeing is that of cout.
Consider:
The output recieved (obviously the exact addresses will be different by machine)Code:#include <iostream> using namespace std; int main() { cout<<cout<<"Hello World"<<endl; void* ptr = cout; cout<<ptr<<endl; cout<<(&cout)<<endl; }
So it appears that cout<<cout; returns a void pointer address.0x804de9cHello World
0x804de9c
0x804de98
Using gdb I was able to confirm that it does in fact call
std::basic_ios<char, std::char_traits<char> >::operator void*() const
the << operator calls operator<< for whatever datatype you pass it so you did call a function.or called any function.
Take for example overloading the << operator . Might make it seem a little clearer.
this is in main of courseCode:Class complex{}; std::ostream& operator<< (std::ostream&, const Complex&);
So in fact you are calling a function, in this case the overloaded << functionCode:Complex b; cout << b; //which is equivalent to the call cout.operator(b)
C++ Rules!!!!
------------
Microsoft Visual Studio .NET Enterprise
You might try reading the manual (scroll down the page to the "return value" section). The return value of a stream is well documented in every c++ textbook and millions of online references. If you don't know what something does, then look it up in the manual before you hurt yourself.Originally Posted by vaibhav
You might want to try reading the thread. The question wasn't what operator<< returned but what cout<<cout displayed. Yea they used return wrongly but thats pretty obvious if you read the post and the replies. Oh and next time you link something at least make sure its relevent. (you linked operator>> which is istreams and we are talking about operator<< which is ostream)Originally Posted by Ancient Dragon
I compiled above program.I get output asCode:#include <iostream> using namespace std; int main() { cout<<cout<<"Hello World"<<endl; void* ptr = cout; cout<<ptr<<endl; cout<<(&cout)<<endl; }
There is one similarity in yours and my output. address returned by cout is exactly 4 bytes greater than address of cout.I think it may help you in finding what cout returns?Code:0x8f910378Hello World 0x8f910378 0x8f910374
My teacher replied this as
Can anyone explain this to me in simple language?Code:In general, s<<a where s is a stream returns a reference to s. cout<<cout; converts the second "cout" to a printable value using the conversion void* that we use for testing its state (see your C++ textbook). That value is only guaranteed to be zero (meaning the state is bad) or non-zero (meaning the state is good). Nothing more is guaranteed.
See this
Borland bcc32 gives the following output (different in nature from the others, but compliant with the actual spec):Originally Posted by Ancient Dragon
D00000001Hello World
00000001
0042049C
Thanks all of you ,Now I understands