有人可以向我解释为什么这个程序会向 std::cout 发送地址吗?
#include<string>
#include<iostream>
#include<fstream>
std::ostream& stuff(std::ostream& o, std::string s)
{
o << s << std::endl;
return o;
}
int main(){
std::cout << stuff(std::cout, "word") << std::endl;
}
这是由 main().. 中的 std::endl 引起的,但为什么呢??
输出:
word
0x804a064
最佳答案
您的函数 stuff
返回传递给它的 std::ostream
。
这意味着您的代码:
std::cout << stuff(std::cout, "word") << std::endl;
实际上会调用:
std::cout << (std::cout) << std::endl;
^^^^^^^^^^^ this is the result of calling "stuff"
您正在输出您的 std::cout
对象的地址。
你的程序在功能上等同于:
std::cout << "word" << std::endl;
std::cout << std::cout << std::endl;
关于c++ - 发送 std::endl 到流给出内存地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31216969/