reference to a pointer

G

graham

Hi,

I need to initialise a reference to a pointer. I'm a client of a
function that requires it.

Can you tell me how I can shorten the initalisation of totoToRef
below? I'm obviously going the long way about it.

void mymethod(toto& totoRef)
{

toto* ptr = &totoRef;
toto*& totoRefToPointer = ptr;

}


thanks and have a nice weekend.
 
S

SG

I need to initialise a reference to a pointer.
I'm a client of a function that requires it.

Can you tell me how I can shorten the initalisation of totoToRef
below? I'm obviously going the long way about it.

void mymethod(toto& totoRef)
{
   toto* ptr = &totoRef;
   toto*& totoRefToPointer = ptr;
}

I see no 'totoToRef' here.

What is the point of creating 'totoRefToPointer'?

Answer: There is none. Whether you use 'ptr' or 'totoRefToPointer' in
following expressions does not matter at all. You basically have one
pointer object and two names that refer to this pointer object, namely
'ptr' and 'totoRefToPointer'.

SG
 
P

Paul

I need to initialise a reference to a pointer.
I'm a client of a function that requires it.

Can you tell me how I can shorten the initalisation of totoToRef
below? I'm obviously going the long way about it.

void mymethod(toto& totoRef)
{
toto* ptr = &totoRef;
toto*& totoRefToPointer = ptr;
}

I see no 'totoToRef' here.

What is the point of creating 'totoRefToPointer'?

Answer: There is none. Whether you use 'ptr' or 'totoRefToPointer' in
following expressions does not matter at all. You basically have one
pointer object and two names that refer to this pointer object, namely
'ptr' and 'totoRefToPointer'.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Typical of you to question the reason SG.
With your philosophical thinking all references are completely unneccessary.
:)

PMSL.
 
G

graham

Not much you can do. You need a pointer, and there isn't one handy, so
you have to make one. If by "I'm a client of a function..." you mean
that the code will call a function that takes a reference to a pointer,
you can just pass ptr; you don't need a named reference to call a
function that takes a reference.

--
  Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

thanks for the reply Pete. My reference to pointer is actually passing
thru a third party API that does a conversion into another type thats
ultimately used.

Im out of the office now so I cant check but Im certain I did try
passing the pointer directly and it failed. The only way I could pass
compile was by doing what I originally posted.

That said it was 17.50 on friday evening and I could have been making
mistakes left right and centre.

Have a nice weekend.

G
 
A

Alf P. Steinbach /Usenet

* graham, on 29.04.2011 17:30:
Hi,

I need to initialise a reference to a pointer.

That's the same as with a non-pointer. E.g.,

int* p1 = 0;
int*& p2 = p1;

I'm a client of a
function that requires it.

That's vague, to say the least.

Can you tell me how I can shorten the initalisation of totoToRef
below? I'm obviously going the long way about it.

void mymethod(toto& totoRef)
{

toto* ptr =&totoRef;
toto*& totoRefToPointer = ptr;

}

There is no `totoToRef` (which you're asking about) above.

`totoRefToPointer` is identically the same as `totoRef`.

This code jumps through hoops to do nothing.



Cheers & hth.,

- Alf
 
G

Gerhard Fiedler

Alf said:
That's vague, to say the least.

I interpreted it to say that he needs to call a function that requires
an argument of type toto*& (from inside a function that only has access
to a toto&).
There is no `totoToRef` (which you're asking about) above.

`totoRefToPointer` is identically the same as `totoRef`.

I didn't understand this affirmation.

Assume

struct toto {
void foo() {}
}

then it seems that the two following would be legal in mymethod:

totoRef.foo();
totoRefToPointer->foo();

Right? If so, how can totoRef and totoRefToPointer be "identically the
same"? (BTW, what's the difference between "identical to", "the same as"
and "identically the same as"?)
This code jumps through hoops to do nothing.

It seems to create a reference to a pointer to an object of which only a
reference to it is accessible.

Gerhard
 
A

Alf P. Steinbach /Usenet

* Gerhard Fiedler, on 18.05.2011 21:09:
I interpreted it to say that he needs to call a function that requires
an argument of type toto*& (from inside a function that only has access
to a toto&).


I didn't understand this affirmation.

Sorry about the typo, it should be "`totoRefToPointer`is identically the same as
`ptr`".

Assume

struct toto {
void foo() {}
}

then it seems that the two following would be legal in mymethod:

totoRef.foo();
totoRefToPointer->foo();

Right? If so, how can totoRef and totoRefToPointer be "identically the
same"? (BTW, what's the difference between "identical to", "the same as"
and "identically the same as"?)

It can't; see above.

It seems to create a reference to a pointer to an object of which only a
reference to it is accessible.

Yes, it's just silly. :)


Cheers & thanks,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top