reference to a pointer

Discussion in 'C++' started by graham, Apr 29, 2011.

  1. graham

    graham Guest

    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.
     
    graham, Apr 29, 2011
    #1
    1. Advertising

  2. graham

    SG Guest

    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
     
    SG, Apr 29, 2011
    #2
    1. Advertising

  3. graham

    Paul Guest

    "SG" <> wrote in message
    news:...
    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.
     
    Paul, Apr 29, 2011
    #3
  4. graham

    graham Guest

    On Apr 29, 5:37 pm, Pete Becker <> 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
     
    graham, Apr 29, 2011
    #4
  5. * 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>
     
    Alf P. Steinbach /Usenet, Apr 30, 2011
    #5
  6. 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
     
    Gerhard Fiedler, May 18, 2011
    #6
  7. * 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>
     
    Alf P. Steinbach /Usenet, May 18, 2011
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sam pal
    Replies:
    3
    Views:
    583
    E. Robert Tisdale
    Jul 16, 2003
  2. Replies:
    10
    Views:
    731
    Chris Torek
    Feb 4, 2005
  3. Replies:
    4
    Views:
    407
    Andre Kostur
    Dec 24, 2007
  4. mathieu
    Replies:
    8
    Views:
    523
    Juha Nieminen
    Aug 31, 2008
  5. A
    Replies:
    7
    Views:
    649
Loading...

Share This Page