Fillups
Fillups

Reputation: 55

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

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?

Upvotes: 3

Views: 54

Answers (1)

songyuanyao
songyuanyao

Reputation: 172934

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.

Upvotes: 5

Related Questions