Reference to pinter

F

Fab

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
 
J

Jorgen Grahn

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
 
G

gwowen

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

Marcel Müller

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
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top