Like others have said, the & means you're taking a reference to the actual variable into the function as opposed to a copy of it. This means any modifications made to the variable in the function affect the original variable. This can get especially confusing when you're passing a pointer, which is already a reference to something else. In the case that your function signature looked like this
void myFunc(myStruct *out);
What would happen is that your function would be passed a copy of the pointer to work with. That means the pointer would point at the same thing, but would be a different variable. Here, any modifications made to *out
(ie what out points at) would be permanent, but changes made to out
(the pointer itself) would only apply inside of myFunc
. With the signature like this
void myFunc(myStruct *&out);
You're declaring that the function will take a reference to the original pointer. Now any changes made to the pointer variable out
will affect the original pointer that was passed in.
That being said, the line
out = new myStruct;
is modifying the pointer variable out
and not *out
. Whatever out
used to point at is still alive and well, but now a new instance of myStruct has been created on the heap, and out
has been modified to point at it.