What is the difference between the following parameter passing mechanisms in C++?
void foo(int &x)
void foo(int *x)
void foo(int **x)
void foo(int *&x)
I’d like to know in which case the parameter is being passed by value or passed by reference.
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.
passes a reference to an integer. This is an input/output parameter and can be used like a regular integer in the function. Value gets passed back to the caller.
passes a pointer to an integer. This is an input/output parameter but it’s used like a pointer and has to be dereferenced (e.g.
*x = 100;). You also need to check that it’s not null.passes a pointer to a pointer to an integer. This is an input/output parameter of type integer pointer. Use this if you want to change the value of an integer point (e.g.
*x = &m_myInt;).passes a reference to a pointer to an integer. Like above but no need to dereference the pointer variable (e.g.
x = &m_myInt;).Hope that makes sense. I would recommend using typedefs to simplify the use of pointers and reference symbols.