One thing that I have not been able to understand is when to use certain types of pointers for arguments in functions.
Consider a function that receives an integer as its parameter, and doubles whatever that value may be. A function for that could be:
void doubleTheValue(int *myNum)
{
*myNum *= 2;
}
int main()
{
int number = 2;
doubleTheValue(&number);
// prints 4
cout << number << endl;
return 0;
}
This makes sense to me. The function receives an integer pointer, and you pass in a reference to the variable 'number' and it changes the value. Now, what confuses me is if you did this instead:
void doubleTheValue(int &myNum)
{
myNum *= 2;
}
int main()
{
int number = 2;
doubleTheValue(number);
// this also prints 4
cout << number << endl;
return 0;
}
Note the argument for the function is different. What exactly is this doing internally, and why would you use it over the aforementioned method?