| guiyii (9) | |
|
What's the problem of this code,I want to use this function to increment the value of win,but it doesn't work at all. #include<iostream> using namespace std; void plus(int); int main() { int win=0; cout<<win<<endl; plus(win); cout<<win; system("pause"); return 0; } void plus(int a) { a++; } | |
|
|
|
| Gulshan Singh (46) | |
|
You're passing a to the function by value, so it makes a copy, increments that copy, and then the copy gets destroyed. You need to pass by reference, like this: void plus(int& a) http://www.cplusplus.com/doc/tutorial/functions2/ | |
|
Last edited on
|
|
| guiyii (9) | |
|
Thanks very much, I've stuck on this for a long time. | |
|
|
|