是否有任何解釋在標準的以下行爲?C++ Odox行爲與ostringstream
下面的代碼:
#include <sstream>
#include <iostream>
using namespace std;
int main()
{
ostringstream os1;
ostringstream os2;
os1 << 1 << " " << 2;
os2 << 1 << " " << 2 << " " << 3;
const char *p = os1.str().c_str();
cout << os2.str() << endl;
cout << p << endl;
return 0;
}
顯示輸出:
不過,我期待它顯示:
1 2 3
1 2
看起來OS1對象以某種方式影響通過os2,如果我刪除os2.str()調用,則示例行爲正確。
我已經嘗試過這個例子,如果Solaris Studio 12.2和G ++ 4.8.1和兩者的行爲方式相同。
感謝您的幫助!
奇怪的是,此代碼在我的機器w/g ++上打印「1 2 3 \ n1 2 \ n」。 – jma127
試試這裏:http://www.compileonline.com/compile_cpp_online。php –
@ jma127我在Debian上使用g ++ 4.8.1得到了同樣奇怪的結果,例如P.An. – PhillipD