Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 633
wwahammy Avatar asked Nov 20 '10 19:11

wwahammy


1 Answers

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.

like image 199
逆さま Avatar answered Sep 25 '22 02:09

逆さま