I started learning C++ in September and am studying up for an exam on Monday.
The correction to a past exam says these two function declarations can cohabitate in a program:
void fct(int&);
void fct(int);
But if you call them in the main, say with...
int i(15);
fct(i);
..how does the compiler know whether to modify i (&) or to modify a copy of i?
Technically, you're allowed to do that. However, I can't possibly think of a time when the referenced version could be called. Passing a constant int would result in the overloaded function to be called. Attempting to pass a non constant variable results in an error. See here:
1 2 3 4 5 6 7 8 9 10
void fct(int &n){}
void fct(int n){}
int main() {
int i(15);
fct(i); // error: call of overloaded 'fct(int&)' is ambiguous
fct(15); // ok
return 0;
}
Like I said, I couldn't begin to imagine when a referenced version would be used over the other one, but it does compile as long as you pass a constant value to the function, otherwise it has no clue.
Note: You mentioned it was a correction. I'm assuming your teacher didn't know that either and someone pointed it out to them. Have you asked your teacher to elaborate?
The teacher is unavailable, but the consensus among students is that the question was just whether or not the declaration is valid, and did not concern the implementation and usage thereafter. I suppose it's an evaluation of only our theoretical understanding.