Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > reference to a pointer

Reply
Thread Tools

reference to a pointer

 
 
graham
Guest
Posts: n/a
 
      04-29-2011
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.

 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      04-29-2011
On 29 Apr., 17:30, graham wrote:
>
> 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
 
Reply With Quote
 
 
 
 
Paul
Guest
Posts: n/a
 
      04-29-2011

"SG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
On 29 Apr., 17:30, graham wrote:
>
> 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.

 
Reply With Quote
 
graham
Guest
Posts: n/a
 
      04-29-2011
On Apr 29, 5:37*pm, Pete Becker <(E-Mail Removed)> wrote:
> On 2011-04-29 11:30:30 -0400, graham said:
>
>
>
>
>
> > 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.

>
> 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
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      04-30-2011
* 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


--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
Gerhard Fiedler
Guest
Posts: n/a
 
      05-18-2011
Alf P. Steinbach /Usenet wrote:

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

>
> 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&).

>> 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`.


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
 
Reply With Quote
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      05-18-2011
* Gerhard Fiedler, on 18.05.2011 21:09:
> Alf P. Steinbach /Usenet wrote:
>
>>> I'm a client of a
>>> function that requires it.

>>
>> 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&).
>
>>> 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`.

>
> 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.


>> 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.


Yes, it's just silly.


Cheers & thanks,

- Alf

--
blog at <url: http://alfps.wordpress.com>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
reference and pointer-reference kasthurirangan.balaji@gmail.com C++ 4 12-24-2007 09:05 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
Passing the value by reference is same as pointer by reference sam pal C++ 3 07-16-2003 09:14 PM



Advertisments