Sign in to follow this  
steveharper

Strange parameter passing in c++

Recommended Posts

ttdeath    100
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
DrEvil    1148
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
bakery2k1    712
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
dave    2187
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
steveharper    122
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
ttdeath    100
Nice going about my rating...

How about trying this first:

#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 this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by ttdeath
Actually 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 this post


Link to post
Share on other sites
ttdeath    100
Quote:
Original post by bakery2k1
@ttdeath

You'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 this post


Link to post
Share on other sites
bakery2k1    712
Quote:
Original post by ttdeath
In 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 this post


Link to post
Share on other sites
MaulingMonkey    1730
Quote:
Original post by ttdeath
Quote:
Original post by bakery2k1
@ttdeath

You'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 this post


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


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


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this