I am trying to use a function looking like this:
function (Item *&)
and I am breaking my head over the '*&'
Does anyone know what it means ?
I am trying to use a function looking like this:
function (Item *&)
and I am breaking my head over the '*&'
Does anyone know what it means ?
a reference to a pointer to an item
"You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter
it is a bit scary, but that was enough info already...
I got it running.
Thanks !
It also means trouble, function is going to change your pointer ugh, do I have to keep a copy to delete it later? Is function going to "helpfully" delete it for me, what if I want to send it a pointer to something on the stack or in an array? Such code is not fit for consumption by the general public. You might come up with an elegant hack somewhere deep in the bowels of your own library, but never expose such things to the poor users, and it's still got bad idea written all over it.
it's usefull for things like linked-lists
lets say you have a linked list that follows the struct like this:
and in the main, you had the pointer to the begining, but you wanted to clear the whole thing, recursively... you would need to be able to change the pointer otherwise when your done... it'll be pointing to a place it shouldn'tCode:struct node { int id; node* next; };
A common way to not get confused is to do something like:
and thenCode:typedef node* nodeptr;
These are the most common uses I can think of right now. Usually changing pointers in a function is a bad idea because you can't be sure how your pointer will return to you.Code:int deletelist(nodeptr &root);
-LC
Asking the right question is sometimes more important than knowing the answer.
Please read the FAQ
C Reference Card (A MUST!)
Pointers and Memory
The Essentials
CString lib