-3
void func(int x) {
x = 2;
}
int main()
{
int x = 3;
func(x);
cout << "x = " << x << endl;
return 0;
}
アウトアウトが2になると予想していますが、これはどうして起こりませんか?私はちょうどC + +を覚え始めたので簡単な説明をしてください。私は2.なぜこの出来事ではないことを置く期待出力が機能を考慮しないのはなぜですか?
void func(int x)
{
x = 2;
}
void function(int *x)
{
*x = 5;
}
int main()
{
int x = 3;
func(x);
function(&x);
cout << "x = "<< x << endl;
return 0;
}
[関数内のパラメータを変更すると、呼び出し元に対しても変更されますか?](http://stackoverflow.com/questions/1698660/when-i-change-a-parameter-inside -a-function-does-it-for-the-caller-too) –
も参照してください。http://stackoverflow.com/questions/11736306/when-pass-a-variable-to-function-why-関数の唯一の複製を取得する、http://stackoverflow.com/questions/21215409/does-c-pass-objects-by-value-or-reference、およびC++の基本チュートリアル関数:http://www.cplusplus.com/doc/tutorial/functions/、ポインタの例:http://cslibrary.stanford.edu/104/(C++バージョン)。 –