程式碼如下
#include <iostream>
using namespace std;
class point
{
public :
int x;
int y;
point()
{
x=0;
y=0;
}
void output()
{
cout<<"x =";
cout<<x<<endl;
cout<<"y =";
cout<<y<<endl;
cout<<"x = "<<cout<<x<<endl;
cout<<"y = "<<cout<<y<<endl;
}
};
int main(int argc, char *argv[])
{
point pt;
pt.output();
return 0;
}
輸入結果:
x = 0
y = 0
x = 0x4453c40
y = 0x4453c40
請按任意鍵繼續. . .
cout<<x<<endl輸出的數字
cout<<"x = "<<cout<<x<<endl;輸出的字串,已經初始化為0,為何是0x4453c40
疑問為何 cout<<x<<endl 與cout<<"x = "<<cout<<x<<endl的輸出會不一樣,請高手告之。
編譯環境為vs2008 與c-free,