Reference aegument

Hi All,

Im a little consfused about reference as function argument.

Say I have a function foo that takes and int reference as argument.

  void foo(int& x);

and main looks like this:


int main()

  int x = 7;
  return 0;

foo takes a reference as its argument yet I pass x value that its not a reference and still the program works...why?

Thank u in advce
Whether a reference or a copy of a cariable passed to a function depends on the function declaration.

For this function declaration

void f( int x );


f( x );

means that a copy of the original variable is passed.

For this function declaration

void f( int &x );


f( x );

means that the same variable is passed not its copy.

But I don't pass it a reference I pass it a variable that been declared as int not int&
If a function takes references as parameters the compiler will pass to the function a reference to any argument instead of a copy of the argument
O I see thge compiler generates a reference on the fly?
More or less yes.
References are implicitly-deferenced pointers. When you have a function that takes a reference and pass an object to it, the compiler sees that the function wants a reference to the object, takes the address of the object and assigns it to a pointer that will always be treated as already deferenced inside the function.
I see Thank u
Topic archived. No new replies allowed.