user4407915
user4407915

Reputation:

Why a reference parameter can be re-seted?

Why I can't re-set the value of a reference but I can do that if the reference is a function parameter? For example the following code will work without a problem:

void foo(int& i)
{
}

int main()
{
    int i; foo(i);
    int j; foo(j);

    return 0;
}

Upvotes: 0

Views: 66

Answers (1)

user207421
user207421

Reputation: 310980

There is no 'reference parameter re-set' here. The function reference formal parameter doesn't even exist until you call the function, with a new actual argument value and possibly a new location on the stack every time you call it. Every time you call the function you are initializing a new reference (to be passed as the actual argument value), just as you would be with int &k = i; in the main() of your example.

Upvotes: 1

Related Questions