Monday, October 23, 2006

When do you need a pointer to a reference?

From C++ groups
> Why/when would someone need a pointer to a reference?

Never. A reference is another name for a real thing. A pointer can only
point to a real thing - it can't point to a name for a real thing.

References are often implemented as secret pointers, but it breaks the
language if you try to get a handle on this secret pointer - it is an
implementation detail.

If you meant a reference to a pointer, use this when you need something to
grab your pointer, point it to something else, and give the result back to
you. Consider a parser that reads statements written by the user:

WORD_TYPE getWord (char *&statement);

Each time you call this function it finds a word, returns its type, and
points the pointer off the end of the word.

No comments: