[英]Does std::cout have a return value?
I am curious if std::cout has a return value, because when I do this: 我很好奇std :: cout是否有返回值,因为当我这样做时:
cout << cout << "";
some hexa code is printed. 打印一些hexa代码。 What's the meaning of this printed value?
这印刷价值的含义是什么?
Because the operands of cout << cout
are user-defined types, the expression is effectively a function call. 因为
cout << cout
的操作数是用户定义的类型,所以表达式实际上是函数调用。 The compiler must find the best operator<<
that matches the operands, which in this case are both of type std::ostream
. 编译器必须找到与操作数匹配的最佳
operator<<
,在这种情况下,它们都是std::ostream
类型。
There are many candidate operator overloads from which to choose, but I'll just describe the one that ends up getting selected, following the usual overload resolution process. 有许多候选运算符重载可供选择,但我只是按照通常的重载解析过程描述最终被选中的那个。
std::ostream
has a conversion operator that allows conversion to void*
. std::ostream
有一个允许转换为void*
的转换运算符。 This is used to enable testing the state of the stream as a boolean condition (ie, it allows if (cout)
to work). 这用于将流的状态测试作为布尔条件(即,它允许
if (cout)
工作)。
The right-hand operand expression cout
is implicitly converted to void const*
using this conversion operator, then the operator<<
overload that takes an ostream&
and a void const*
is called to write this pointer value. 使用此转换运算符将右侧操作数表达式
cout
隐式转换为void const*
,然后调用operator<<
supers,它接受一个ostream&
和一个void const*
来写入此指针值。
Note that the actual value resulting from the ostream
to void*
conversion is unspecified. 请注意,未指定
ostream
to void*
转换产生的实际值。 The specification only mandates that if the stream is in a bad state, a null pointer is returned, otherwise a non-null pointer is returned. 规范仅强制要求如果流处于错误状态,则返回空指针,否则返回非空指针。
The operator<<
overloads for stream insertion do have a return value: they return the stream that was provided as an operand. 用于流插入的
operator<<
overloads确实有一个返回值:它们返回作为操作数提供的流。 This is what allows chaining of insertion operations (and for input streams, extraction operations using >>
). 这允许链接插入操作(以及输入流,使用
>>
提取操作)。
cout
does not have a return value . cout
没有返回值 。 cout
is an object of type ostream
. cout
是ostream
类型的对象。 operator <<
has a return value, it returns a reference to cout
. operator <<
有一个返回值,它返回对cout
的引用。
See http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/ for reference. 请参阅http://www.cplusplus.com/reference/iostream/ostream/operator%3C%3C/以供参考。
The only signature that matches is: 唯一匹配的签名是:
ostream& operator<< (ostream& ( *pf )(ostream&)); ostream&operator <<(ostream&(* pf)(ostream&));
so it returns the pointer to the operator<<
member. 所以它返回指向
operator<<
member的指针。
the one in James' answer. 詹姆斯答案中的一个。 :)
:)
我相信这将是“”打印到的ostream对象的地址
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.