What is actually passed in call by reference to a function?
void foo(int &a,int &b)
when I write
foo(p,q)
what is actually passed to the function. Is it the address of p and q?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What’s actually passed to the function is a reference. The named parameter
bbecomes a synonym for the argument objectq.How the compiler probably implements this that the caller places the address of
qon the stack or in a register before calling, and the callee uses that value to effect all accesses tob. But it could be misleading to describe that as “actually passing” a pointer, because parameter passing is a concept at the level of the C++ language, and at that level it is not the same concept as passing a pointer. For instance, when you pass a pointer you can pass a null pointer, but when you pass a reference you cannot (validly). So it’d be wrong to say they’re same thing.That said, the person implementing the compiler might describe it as “actually passing a pointer”, and you know what they mean. For comparison, if
charvariables occupy 4-byte stack slots in the calling convention, they might say that the compiler is “actually passing an int”. So it depends what “actually” is supposed to mean.