How does the cout object print multiple arguments?
IOStreams only takes one argument at a time, so it works great. :)
The magic of operator overloading means this:
std::cout << a << b << c;
actually it is:
std::operator<<(std::operator<<(std::operator<<(std::cout, a), b), c);
or that:
std::cout.operator<<(a).operator<<(b).operator<<(c);
(Depending on the types a
, b
and c
either the free function or the member function will be called.)
and every single call is an overload that takes on the type you give it. No strings are required for argument counting or formatting, since they are associated with your single calls printf
.
source to share
<<
and are >>
overloaded for different data types. In this case, there is no need for a format specifier. For an operator, the <<
following definitions are in the class ostream
:
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
ostream& operator<< (streambuf* sb );
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));
source to share
There is no implementation in C ++ streams for multi-value I / O. They have formatted I / O operators -> / <<that separate each output value. Apart from them, they have unformatted functions that operate on one value (array)
Example: single ostream& operator << (T a);
puts the value 'a' on a stream and returns a stream reference itself. This stream reference is allowed to accept the next "b" value, as instream << a << b;
Note. This only compiles for certain operators IStream& operator >> (IStream&, Type)
and OStream& operator << (OStream&, Type)
. A specific operator can be an operator provided by the standard library or a user-defined operator.
source to share