I have a method with the prototype:
bool getAssignment(const Query& query, Assignment *&result);
I am a bit confused about the type of the second param (Assignment *&result) since I don’t think I have seen something like that before. It is used like:
Assignment *a;
if (!getAssignment(query, a))
return false;
Is it a reference to a pointer or the other way around ? or neither ? Any explanation is appreciated. Thanks.
It’s a reference to a pointer. The idea is to be able to change the pointer. It’s like any other type.
Detailed explanation and example:
will not change
p_mainto point to the allocated char array (it’s a definite memory leak). This is because you copy the pointer, it’s passed by value (it’s like passing anintby value; for examplevoid f( int x )!=void f( int& x )) .So, if you change
f:now, this will pass
p_mainby reference and will change it. Thus, this is not a memory leak and after the execution off,p_mainwill correctly point to the allocated memory.P.S. The same can be done, by using double pointer (as, for example,
Cdoes not have references):