For example:
void do_something(int& x){
//this function asks for a reference to an int,
//since the type of its argument is int&
}
int main() {
int x = 4; //the variable x is simply an int; it isn't a reference?
int& y = x; //the variable y is a reference to x
do_something(y); //this works, as expected
do_something(x);
//this calls the method with an int, instead of a reference.
//Why does this work? It's not giving the function what the function is
//asking for.
}
Why does do_something(x)
work? It's not giving the function what the function is asking for. The only explanation I can come up with is that the int being passed to the function has a reference created for it, and that reference is what ends up being passed to the function.