Copies vs References
Completed
Given struct Score { int value; };, write void bumpCopy(Score s) and void bumpRef(Score &s) that add 10. Read a value, call both, and print the value after each call.
Input
One integer.
Output
Two lines.
Example
Input
5
Output
5 15
Editor
C++
Hints
Objects are passed by value (copied) unless you use a reference.
Solution explanation
Unlike Java, C++ copies whole objects when you pass them by value.
Show solution
#include <iostream>
using namespace std;
struct Score {
int value;
};
void bumpCopy(Score s) { s.value += 10; }
void bumpRef(Score &s) { s.value += 10; }
int main() {
Score s;
cin >> s.value;
bumpCopy(s);
cout << s.value << "\n";
bumpRef(s);
cout << s.value << "\n";
return 0;
}