Firstly please have a look at some simple codes that my questions derived from.
#include <iostream>
#include <string>
using namespace std;
string get_something()
{
cout << "output something";
return " and return something";
}
void print_something()
{
cout << "print something";
}
int main()
{
cout << get_something(); // will work
cout << print_something(); // will NOT work
return 0;
}
The only different thing I notice between get_something() and print_something() is that one is a return type and one isn't. As you can see I have added comments indicating that which one will work and not work.
However, I am really not sure what is happening behind the scene that makes it one possible and the other not possible. I am not even sure how I should go about and search for this kind of question too.. so here I am asking a question.
Please enlighten me..
edit: I am confused that how it is possible to do cout after cout.. both of the functions do that but one of them works and the other doesn't.
This seems to be a very common misunderstanding among beginners. Printing something via cout
is not the same as returning a value from a function. Thats completely orthogonal things.
You can write:
std::string returned_value = get_something();
std::cout << returned_value;
But you cannot write:
??? returned_value = print_something();
std::cout << returned_value;
because print_something()
does not return anything! void
denotes the absence of a type. You cannot have an object of type void
.
On the other hand, when you call a function, you can use the returned value (above), or you can ignore it, so this is correct code:
print_something(); // prints something
get_something(); // also print something and returned value is ignored
Note that the function get_something
should get a better name, because it is not just "getting" a value. How about print_and_return_something()
?
PS:
What I am really confused about is that, how is it possible to do a cout after a cout? Am I just missing what cout actually does?
Not sure If I understand, but I will try... std::cout
is an object of type std::ostream
. It has an operator<<
that you can call, similar to calling methods of other objects. The following two are identical and just use different syntax:
std::cout.operator<<( "Hello World");
std::cout << "Hello World";
When you call print_something()
then first the function is executed, then the return value is returned to the caller and execution continues with the caller. This:
std::cout << get_something();
is more or less the same as (well, its a crude simplification, but should be ok here):
// inside get_something
std::cout << "output something";
// return value
std::string result{"output something"};
// now execution continues in caller
std::cout << result;
Calling cout
after cout
is no different from calling some other function. Suppose you have a function print()
that prints something then you can write
std::string print_and_return() {
std::string x{"Hello World"};
print(x);
return x;
}
The caller can do
std::string x = print_and_return(); // <- this already calls print()
print(x); // now we call it again
This is more or less the same as yours, just that I used some hypothetical print()
instead of std::cout::operator<<
.
Both your functions have a return type. It's just that one of them has a void
return type.
The std::ostream
class does not have an overload for <<
that takes a void
type. This is sensible - what would be written to the stream in that case?
( cout
is an instance of std::ostream
that typically writes itself to the standard output which is normally the shell you're using to launch the program.)
Because print_something()
has nothing to return, and cout
want something to write to the console (the return value it is expecting). Therefore, it will give error.
get_something()
, on the other hand, has something to return. So after executing it's rest of line (except return statement) it return the string, which gets printed by cout
get_something()
returns something (what seems to be accepted by cout
), so cout
will receive the returned thing and will work.
On the other hand, print_something()
returns nothing (because its return type is void
), so cout
cannot receive anything to print and won't work.
cout
is a stream object.and we use <<
(insertion operator) to insert value like String,float,Int etc to it which will be displayed in output Screen.Since print_something()
is not returning any value so nothing is inserted in stream,That's why it is not working.
I recommend you to read about Streams in c++..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.