You learn something new every day

I frequently see function prototypes written as
void function(type& param);
but until today have never found out what the relevance of the & was. I assumed it was the opposite of a *, a dereference, but didn’t understand how that worked. I always treated functions like this as if they were written
void function(type param);
and no harm came of it.
But, looking at a physX function early that was
void NxBoxShape::saveToDesc(NxBoxShapeDesc& desc)

I wondered how it could return from this. I had the additional problem that I had a pointer I wanted it to save the desc to, of type BYTE*. I tried various casts that didn’t work, and after a while on Bing I found out that this type of parameter takes a normal variable as an argument, but within the function treats it as a reference, so the value can be changed and it will affect the argument.
It is odd that I never knew this before, either I missed it in the various tutorials and books I read, or no-one thinks it worthwhile to mention. Well, now I know.
In the end, I solved my little problem by just passing a new NxBoxShapeDesc then using CopyMemory to save it to the pointer.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *