For function declaration like
ostream& operator<< (ostream& os, const unsigned char* s);
I am wondering what has been returned. The CPP reference says it returns ostream object. But why it is ostream& instead of simple ostream?
Thank you
The reason that the operator returns a ostream&
(i.e. a modifiable reference to an ostream
object), rather than a copy or void is that it allows for chaining, for instance, a common example with std::cout
as the ostream
object:
unsigned int i = 2;
std::cout << "This is a test to print " << "some text and maybe some numbers: " << i << std::endl;
Here we chain two const char*
s, an unsigned int
and a stream modifer, without having to seperate them with separate lines, this makes it nicer to read and understand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With