次のプログラムを実行し、変数x、yに及ぼすスコープ解釈演算子の働きに注目しながら、出力された各行の値の理由を考えてみよ。
#include<iostream> using namespace std; int x = 10; int main(){ int x = 20; int y; y = ::x; cout << "x = " << x << " y = " << y << "\n"; if(x>5){ int y = 30; cout << "x = " << x << " y = " << y << "\n"; } cout << "x = " << x << " y = " << y << "\n"; for(int x=1; x<4; x++){ int y = x + 1; cout << "x = " << x << " y = " << y << "\n"; } cout << "x = " << x << " y = " << y << "\n"; }
このページへのコメント
SzdCB5 Im thankful for the blog post. Great.