Jump to content
  • Advertisement
Sign in to follow this  
steveharper

Strange parameter passing in c++

This topic is 4597 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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".

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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, **.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!