Strange parameter passing in c++

Started by
19 comments, last by SiCrane 18 years, 4 months ago
Thanks guys, I can see that been really usefull, not sure how i managed not knowing that.

Steve
Advertisement
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.
A trick I ususally use in this situation is to start reading from right to left:

int*&

reference to pointer to int.
my-eulogy - A blog about coding and gfxsdgi - Semi-Daily Game IdeaChunkyHacker - Viewer for Relic chunky formats (used in DOW)
@ttdeath

You've only forced that to work by completely changing the type of b (via a cast) in the function call.
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.
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.
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?

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 );}
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
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.

This topic is closed to new replies.

Advertisement