What does the parameter type void*& mean and what's its use?

wwahammy picture wwahammy · Nov 20, 2010 · Viewed 11.1k times · Source

I'm looking through an API written in C++ and I'm confused as to what the following parameter type means:

void*& data

Does that mean the user would pass in a reference to a void pointer? If that's true, what's the point? I mean void* is already indirected so why would you ever want to redirect it again?

Answer

逆さま picture 逆さま · Nov 20, 2010

void * means pass-by-pointer in C++, meaning you pass in a pointer, but it's actually a copy of the pointer. If you modified this pointer in your function, like changing its address, it is not reflected in the pointer you passed in.

Combining that with pass by reference, void *& means you are passing an alias of the original pointer to this function. If you modified it, like changing its address, it will be reflected in the original pointer.