Is declaring a reference and passing it in the same as just passing by reference?

Viewed 47

Say you have a function like this:

void func(int & arg) {
    std::cout << arg << std::endl;
}

Is doing something like:

int x = 10;
int & y = x;

func(y);

the same as:

int x = 10;

func(x)

Does arg become a int && arg or does it stay a int & arg when you pass in a pre-declared reference like the first case?

1 Answers

Yes they're same. As reference,

Declares a named variable as a reference, that is, an alias to an already-existing object or function.

You can consider y as an alias of x, passing y to the function func has the same effect as passing x directly.

Related