简体   繁体   English

在 cout 语句中使用 cout 调用 function

[英]call function with cout inside a cout statement

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.我在 get_something() 和 print_something() 之间注意到的唯一不同之处在于,一个是返回类型,一个不是。 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.我什至不确定我应该如何 go 并搜索这类问题。所以我在这里问一个问题。

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.编辑:我很困惑如何在 cout 之后执行 cout .. 这两个功能都可以做到,但其中一个有效,另一个无效。

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.通过cout打印内容与从 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!因为print_something()不返回任何东西! void denotes the absence of a type. void表示没有类型。 You cannot have an object of type void .您不能拥有void类型的 object 。

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:另一方面,当您调用 function 时,您可以使用返回值(如上),也可以忽略它,因此这是正确的代码:

 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.请注意,function get_something应该有一个更好的名称,因为它不仅仅是“获取”一个值。 How about print_and_return_something() ? print_and_return_something()怎么样?

PS: PS:

What I am really confused about is that, how is it possible to do a cout after a cout?我真正感到困惑的是,怎么可能在一个 cout 之后做一个 cout? Am I just missing what cout actually does?我只是错过了 cout 的实际作用吗?

Not sure If I understand, but I will try... std::cout is an object of type std::ostream .不确定如果我理解,但我会尝试... std::cout是一个类型为std::ostream的 object 。 It has an operator<< that you can call, similar to calling methods of other objects.它有一个可以调用的operator<< ,类似于调用其他对象的方法。 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.当您调用print_something()时,首先执行 function,然后将返回值返回给调用者,并由调用者继续执行。 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.cout之后调用cout与调用其他一些 function 没有什么不同。 Suppose you have a function print() that prints something then you can write假设你有一个 function print()打印一些东西然后你可以写

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<< .这或多或少与您的相同,只是我使用了一些假设的print()而不是std::cout::operator<<

Both your functions have a return type.您的两个函数都有返回类型。 It's just that one of them has a void return type.只是其中一个具有void返回类型。

The std::ostream class does not have an overload for << that takes a void type. std::ostream class 没有采用void类型的<<重载。 This is sensible - what would be written to the stream in that case?这是明智的 - 在这种情况下,将向 stream 写入什么?

( 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.) coutstd::ostream的一个实例,它通常将自身写入标准 output ,这通常是您用来启动程序的 shell 。)

Because print_something() has nothing to return, and cout want something to write to the console (the return value it is expecting).因为print_something()没有要返回的内容,并且cout想要将某些内容写入控制台(它期望的返回值)。 Therefore, it will give error.因此,它会给出错误。

get_something() , on the other hand, has something to return.另一方面, get_something()有一些东西要返回。 So after executing it's rest of line (except return statement) it return the string, which gets printed by cout所以在执行它的 rest 行之后(除了 return 语句)它返回字符串,由 cout 打印

get_something() returns something (what seems to be accepted by cout ), so cout will receive the returned thing and will work. get_something()返回一些东西(似乎被cout接受的东西),所以cout将接收返回的东西并且会工作。

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.另一方面, print_something()什么也不返回(因为它的返回类型是void ),所以cout不能接收任何要打印的东西并且不会工作。

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. 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 ,这就是为什么它不起作用。

I recommend you to read about Streams in c++..我建议您阅读 c++ 中的 Streams..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM