How does the << operator works??
How does the << operator works??
I'm not sure what you're asking.
http://gethelp.devx.com/techtips/cpp.../10min0400.asp
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
>How does the << operator works??
This gets my vote for the "innocent question with a REALLY long and complicated answer" award.
My best code is written with the delete key.
>How does the << operator works??
Like Prelude said the answer is long and complicated. Dave's link will show you how to make an inserter for your own objects.
Making the answer not so complicated... the stream inserter is an operator that takes a reference to the stream itself and a const reference of the data you want to insert, and writes it into the stream. Exactly how this is done depends on what kind of stream it is, and what the data is, and if any stream manipulators were used on that data.
Since cout and friends are created before you ever use it, and destructed automagically by the compiler, you don't need to worry about it. cout has an inserter for all built in types.
thanks a lot to the 3 of you.
Basically it calls a member function for the stream called operator<< to do the actual ouput. Thus:
Is equivalent to:Code:double foo = 18.9; cout << foo;
Code:double foo = 18.9; cout.operator<<(foo);
This member function is overloaded to handle all the basic primary data types (ints, floats, chars, doubles, char *, etc...). Additional user defined types (classes,structs) can also be created and the operator<< function overloaded to support the writing of those particular object to the stream in question.
The function's return type is a reference to the modified stream which allows them to be chained together thus you can do:
which is equivalent to three seperate calls:Code:double foo = 18.9; char * bar = "Hello World"; cout << foo << ' ' << bar;
Yeah... this can get a bit involved but that's a quick and dirty primer.Code:double foo = 18.9; char * bar = "Hello World"; cout.operator<<(foo); cout.operator<<(' '); cout.operator<<(bar);
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens