f



Pointer to pointer or reference to pointer

The way I understand it Reference To pointer as function parameter eg.

void RP(int *&rpMem) {}

rpMem is actually an alias for the pointer outside the function right?

So I would access it exactly with the same syntax just like outside pointer 
right? 


0
A
7/5/2011 3:50:28 PM
comp.lang.c++ 49423 articles. 7 followers. Post Follow

8 Replies
694 Views

Similar Articles

[PageSpeed] 42

e.g.

void RP(int &*rpMem)
{
*rpMem = 125;
rpMem++;
}

int Mem = 123;
int *pMem = &Mem;        // pMem = for example 5000

*pMem = 124;    // Mem = 124;

RP(pMem);    // Mem = 125, pMem = 5004

Is the above correct? 


0
A
7/5/2011 3:53:27 PM
A <a@a.a> wrote:
> e.g.
> 
> void RP(int &*rpMem)
> {
> *rpMem = 125;
> rpMem++;
> }
> 
> int Mem = 123;
> int *pMem = &Mem;        // pMem = for example 5000
> 
> *pMem = 124;    // Mem = 124;
> 
> RP(pMem);    // Mem = 125, pMem = 5004
> 
> Is the above correct? 

  How about just trying it?
0
nospam270 (2948)
7/5/2011 4:04:42 PM
>  How about just trying it?

Don't you just love these kind of answers...

I did try it and now I'm just verifying and trying to make sense of all this 
how to understand it the easiest way.

ref-to-ptr = alias right? 


0
A
7/5/2011 4:25:38 PM
Le 05/07/2011 18:25, A a �crit :
>>   How about just trying it?
>
> Don't you just love these kind of answers...
>
> I did try it and now I'm just verifying and trying to make sense of all this
> how to understand it the easiest way.
>
> ref-to-ptr = alias right?
>
>

Yes it is.
0
ekinox1995 (22)
7/5/2011 5:57:33 PM
"A" <a@a.a> writes:

> ref-to-ptr = alias right? 

No. ref = alias. Always. Including when referencing pointers. Your first
interpretation was correct (iirc), but you really do not need a specific
case for pointers, it works just the same as for any other kind of type.

-- Alain.
0
alain14 (278)
7/5/2011 7:26:45 PM
A wrote:
>> How about just trying it?

I think he assumes you never tried it because it doesn't compile.

"Trying" it in codepad (http://codepa.org) I get the following error:

Line 1: error: cannot declare pointer to 'int&'
compilation terminated due to -Wfatal-errors.

This OTOH would work: void RP(int*& rpMem);


( int& *name )...

name is a pointer to a reference, and there is no such
thing in c++.

> Don't you just love these kind of answers...

[snip]

> ref-to-ptr = alias right?

Yes, you can see it as an alias to T that at least has to be
initialized with a valid T (where T is a pointer in your case).


See:

http://www.parashift.com/c++-faq-lite/references.html

and also:

http://en.wikipedia.org/wiki/C%2B%2B_reference

Regards,

Werner

0
werasm (308)
7/5/2011 7:47:10 PM
OK, thank you. I was looking for a way how to remember that. 


0
A
7/5/2011 7:47:31 PM
> ( int& *name )...

sorry, that was a typo.


0
A
7/5/2011 7:49:00 PM
Reply: