Reference to pinter

Discussion in 'C++' started by Fab, May 29, 2013.

  1. Fab

    Fab Guest

    Dear all

    I was browsing the OpenFOAM sources a little and I stumbled over types
    of the form

    T * Tp ( 0 );
    T *& Tpr( Tp );

    In what particular situation would something like this be useful? I can
    only think of that the reference must be initialized?

    I am thankful for any feedback!

    Thanks
    Fab
    Fab, May 29, 2013
    #1
    1. Advertising

  2. Fab

    Jorgen Grahn Guest

    On Wed, 2013-05-29, Fab wrote:
    > Dear all
    >
    > I was browsing the OpenFOAM sources a little and I stumbled over types
    > of the form
    >
    > T * Tp ( 0 );
    > T *& Tpr( Tp );
    >
    > In what particular situation would something like this be useful?


    In the situation that applies in OpenFOAM, maybe? What does the rest
    of the code tell you?

    That said, it seems to me this just creates another (badly chosen)
    name "Tpr" for Tp. I fail to see the usefulness of that, but perhaps
    I'm missing something.

    Sometimes I create references for readability or constness reasons, e.g.

    map<int, string>::iterator it = foo();
    const string& name = it->second;
    // use name instead of it->second

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
    Jorgen Grahn, May 29, 2013
    #2
    1. Advertising

  3. Fab

    gwowen Guest

    On May 29, 7:46 am, Fab <> wrote:
    > In what particular situation would something like this be useful?


    Maybe in Old Times, after a Tea Party, or A Night Out. Or perhaps by
    The Caretaker, The Lover, The Dumb Waiter or any time you fell A
    Slight Ache for an adaption of A Remembrance Of Things Past.
    gwowen, May 29, 2013
    #3
  4. On 29.05.13 08.46, Fab wrote:
    > I was browsing the OpenFOAM sources a little and I stumbled over types
    > of the form
    >
    > T * Tp ( 0 );
    > T *& Tpr( Tp );
    >
    > In what particular situation would something like this be useful? I can
    > only think of that the reference must be initialized?


    I am using this from time to time.

    Example 1:
    // Array of pointers to T, that owns it's elements.
    template <class T>
    class my_vector<T>
    {
    ...
    // Add initial (NULL) element
    T*& append();
    };

    class my_element
    { ...
    };

    my_vector<my_element> array;
    array.append() = new my_element();


    Example 2:

    class my_queue
    {
    entry* Head;
    ...
    void insert(entry* elem, entry* after)
    { entry*& qp = after ? after->Next : Head;
    entry->Next = qp;
    qp = elem;
    }
    }


    Marcel
    Marcel Müller, May 30, 2013
    #4
    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. Darren
    Replies:
    0
    Views:
    495
    Darren
    Oct 11, 2004
  2. integer to pinter conversion

    , Sep 14, 2005, in forum: C Programming
    Replies:
    31
    Views:
    907
    Joe Wright
    Sep 20, 2005
  3. integer to pinter conversion

    , Sep 14, 2005, in forum: C Programming
    Replies:
    3
    Views:
    314
    Irrwahn Grausewitz
    Sep 14, 2005
  4. JoeC

    pinter = string.

    JoeC, Jun 21, 2007, in forum: C++
    Replies:
    6
    Views:
    348
    Jim Langston
    Jun 23, 2007
  5. Cuthbert
    Replies:
    3
    Views:
    278
    James Dow Allen
    Mar 18, 2011
Loading...

Share This Page