Strange parameter passing in c++

Started by
19 comments, last by SiCrane 18 years, 4 months ago
I have been learning C++ for a long while now, and have come across the strange parameter type *&. Anyone have an idea of what this means? Thanks, Steve
Advertisement
A reference to a pointer.
Hi bud,

This is passing by reference:
void foo( int& myInteger ){}


Hope that helps,

ace
Actually a pointer to a reference, me thinks.

If you have a variable reference called "A",
you should be able to send it in the function call
using: "&A".
So this would be used to make a reference passed into a function point to some object used in a function, that is available globally?

Thanks,

Steve
Quote:Original post by bakery2k1
A reference to a pointer.


Useful for when you want your function to be able to set the value of the pointer for the caller. Alot easier to follow than using a pointer to a pointer, **.
Quote:Original post by ttdeath
Actually a pointer to a reference, me thinks.


Nope. You can't have a pointer to a reference, since taking the address of a reference yeilds the address of the item referred to.

Quote:
So this would be used to make a reference passed into a function point to some object used in a function, that is available globally?


It is used to pass a pointer into a function, by reference, so that the function can change the value of the pointer. eg

int main(){    int * pa;    foo(pa);    //...}void foo(int *& pa){    pa = new int;}
You are passing a pointer by reference, which means that you use the pointer inside the function as if it was the pointer that got passed in, not a copy of the pointer.

So you can use it as you would have where you instantiated it.

ace
So let me see if i have got this right. *& creates an alias for the pointer, and this allows you to alter where the pointer point's to inside the function, as oppose to only allowing the memory to be accessed where the pointer points(Because normally a copy of the pointer is passed).

Thanks,

Steve
Sounds right. [smile]

This topic is closed to new replies.

Advertisement