【发布时间】:2016-10-28 03:31:52
【问题描述】:
这是 Visual Studio 中的一个控制台应用程序,所以我想看看如果我在 cout 中调用 cout 会发生什么。它有点工作,但它删除了一个有点奇怪的角色。因此,它从cout 中的string 中删除了main 中的字符数量。因此,它将尽可能多的字符删除到 doPrint() 函数的返回值。
示例: 如果返回值为 1,它将输出“AAAABLLLLLLLLLL” 如果返回值为 2,它将输出“AAAAALLLLLLLLLL”
#include "stdafx.h"
#include <iostream>
int doPrint()
{
std::cout << "AAAAA" << std::endl;
return 1;
}
int main()
{
std::cout << "BBLLLLLLLLLL" + doPrint() << std::endl;
int x;
std::cin >> x;
return 0;
}
这没什么大不了的,但我想知道为什么会这样。 已经谢谢了。
P.S:我知道我应该使用<< 而不是+
【问题讨论】:
-
"BBLLLLLLLLLL" + doPrint()当然不会像您认为的那样做。阅读指针算法。 -
这个可以简化为
int main() { std::cout << "ABC\n" + 1; }