Strange parameter passing in c++

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

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 on other sites
A reference to a pointer.

Share on other sites
Hi bud,

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

Hope that helps,

ace

Share on other sites
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 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 on other sites
Quote:
 Original post by bakery2k1A 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 on other sites
Quote:
 Original post by ttdeathActually 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 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 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 on other sites
Sounds right. [smile]

Share on other sites
Thanks guys, I can see that been really usefull, not sure how i managed not knowing that.

Steve

Share on other sites

#include "stdafx.h"void Say(int *&c){	printf("Val = %d\n", c);}int _tmain(int argc, _TCHAR* argv[]){	int a = 3;	int &b = a;	a = 7;    	Say((int *&)b);	return 0;}

Thanks.

Share on other sites
A trick I ususally use in this situation is to start reading from right to left:

int*&

reference to pointer to int.

Share on other sites
@ttdeath

You've only forced that to work by completely changing the type of b (via a cast) in the function call.

Share on other sites
Quote:
 Original post by ttdeathActually a pointer to a reference, me thinks.

A pointer to a reference, or reference to a reference, is illegal in C++ as well as redundant. C++ references by nature are immutable in terms of what they "point to", and thus both creating a copy of and creating a pointer/reference to a reference (were it legal) have the same effect - an unchanging reference/pointer to the given variable.

Share on other sites
Quote:
 Original post by bakery2k1@ttdeathYou've only forced that to work by completely changing the type of b (via a cast) in the function call.

True. Yet it may be more close to what the poster is asked to do.
Remember that he said he had a '*&' parameter in a function.

In any case, I wouldn't recommend such twisted parameter passing.
Instead, one should make a copy into a local object and then pass the
address of the resulting object to the function via pointer. But I can see
where a pointer to a reference may be needed. And the fact that it works,
typecast or no typecast, it means that it is valid, at least for Visual C compiler. I'm not sure if this is ANSI or ISO compliant, though.

Share on other sites
Quote:
 Original post by ttdeathIn any case, I wouldn't recommend such twisted parameter passing.

Passing a pointer by reference is no more "twisted" than passing any other object by reference. Do you recommend not using references at all?

Share on other sites
Quote:
Original post by ttdeath
Quote:
 Original post by bakery2k1@ttdeathYou've only forced that to work by completely changing the type of b (via a cast) in the function call.

True. Yet it may be more close to what the poster is asked to do.
Remember that he said he had a '*&' parameter in a function.

Right - a reference to a pointer. In this case, you've casted an integer to a pointer (equivilant to reinterpret_cast) and then passed this poniter by reference to the function. The fact that printf prints the desired number is an aftereffect of that C function's total lack of type safety (basically it reinterpret_cast's back to an integer).

Quote:
 But I can see where a pointer to a reference may be needed. And the fact that it works, typecast or no typecast, it means that it is valid, at least for Visual C compiler. I'm not sure if this is ANSI or ISO compliant, though.

The code snippet you've posted is valid C++. That dosn't mean it does what you appear to think it does.

Want proof? Try the illegal syntax for a pointer to a reference:

int main () {	int foo;	int & ref = foo;	int &* example = & ref; // .\main.cpp(4) : error C2528: 'example' : pointer to reference is illegal}

Error message pumped out of MSVC++ 2k5 Express for your viewing pleasure.

A warmup for understanding references to pointers:

int main () {    int a, b;    int * pointer = &a;    int *& reference_to_pointer = pointer;    reference_to_pointer = &b;    assert( pointer == &b );    pointer = &a;    assert( reference_to_pointer == &a );}

Share on other sites
This is what i inteded to use the *& for, in my inital post:

I am using this parameter passing method in a search function of a binary search tree class. This is because when a key is found in the tree that matches the one i need, i need to somehow set a node parameter in the function call equal to the Node found in the tree(So that i can perform methods on it's instance, and update that part of the tree). Because the function uses recursion i cannot eaisly return a pointer to the global data.

I think this is the most efficiant way the go about it, and can't see how it can be done any other way. I just wanted to be 100% sure that parameter passing style achieved what i understood as been correct.

Thanks everyone for you help
greatly appreciated,

Steve

Share on other sites
all right, it may be my bad understanding of how
the reading of the stuff works.

anyway, the poster said:
Quote:
 ...and have come across the strange parameter type *&
.
To me that translates into a pointer to a reference, not as
Mauling Monkey's source goes:
Quote:
 int &* example = & ref;
.

A pointer to a reference looked twisted to me because it is rather unnecessary.
You can either use my given solution with the local copy (if security is an issue)
or another simple reference, or by pointer to pointer.

It would seem I don't get the reading order correctly. I must revisit this thread
when I come back from my headache.

Share on other sites
Pointers, references and cv qualifications get red right to left, not left to right (with the exception of the inner most cv qualification which can be on either side of the base type).

Ex:
int * const & is a reference to a const pointer to int.

const int * * const * volatile & is a reference to a volatile pointer to a const pointer to pointer to const int. It's equivalent to int const * * const * volatile &