Every time I try to compile my code I get error:
cannot convert parameter 1 from \'int *\' to \'int *&\'
The test code looks like this:
The problem is, int*&val
can only be passed an lvalue, which the result of &myVal is not. By changing the signature to void set(int* const& val)
, it's telling the compiler you're not going to change the value of the pointer.
However, you normally wouldn't do that, only because if you're not going to change the value of the pointer, then passing the pointer by value is the most straightforward way to pass the value. And if you are going to change the value of the pointer, then you need to create a temporary to receive the result.