Pointer to reference

13,103

Solution 1

A pointer reference is exactly what it says, a reference to a pointer.

Consider what we know about references. A reference in C++ is a variable that refers to an existing variable elsewhere:

int x = 1;
int &y = x;    // <-- y refers to x. Any changes to y will update x as well, and vice versa.

Also consider what we know about pointers. A pointer points to another object in memory:

int *m = new int(5);   // Pointer to an allocated integer in memory.
int *n = m;            // Pointer to the same memory.

So in your case what you actually have is a reference to a pointer!

int *m = new int(5);   // Pointer to an allocated integer in memory.
int *ptr = m;          // Pointer to m.
int *&ptrRef = ptr;    // Reference to ptr.

In the example above, changing ptrRef would update the pointer, but not the value.

Here's a bit more of a complete example:

int *myPtr = new int(5);   // myPtr points to an integer.

...

void ChangePointer(int *&ptr)
{
    delete ptr;
    ptr = new int(6);
}

...

std::cout << *myPtr << std::endl;  // <-- Output "5"
ChangePointer(myPtr);
std::cout << *myPtr << std::endl;  // <-- Output "6"

In the example above, we pass myPtr to the ChangePointer by reference so that it can be modified by the function. If we did not pass by reference, any changes made inside the function would be lost.

In your case, you're passing a reference to a const pointer. This is approximately equivalent to:

DoStuff(const Object &myObject);

In your case, you're passing a pointer, rather than an object though.

It seems a bit redundant to pass a const pointer by reference though. The pointer cannot be changed (it is const), and there is no benefit to passing pointers by reference (pass by reference is no more efficient than pass by value for small objects like pointers and integers). I wouldn't want to guess as to why it was done in your case.

Solution 2

cdecl is an useful online tool to help beginners get used to complicated declarations.

Inserting const char* const& i returns:

declare i as reference to const pointer to const char

The meaning of the declaration should be obvious now.

Solution 3

Let's take this apart:

  • the trailing & means this is a reference to whatever the type is.

  • const char is the type that's being pointed to

  • * const means the pointer is constant

So, this is a reference to a const pointer to a const char. You can't change the char that it points to (unless you cast away the constness), and you can't change the pointer (i.e. make it point to something else). The pointer is passed by reference, so that no copying occurs.

Solution 4

The other answers largely cover the semantics of references to pointers.

But what if you were in doubt: is it a reference to pointer or pointer to reference? It can be confusing! However, C++ does not allow pointer to references!

Pointers store the address of objects and references are not objects, so there cannot be any pointer to reference.

Share:
13,103

Related videos on Youtube

MEMS
Author by

MEMS

Updated on June 06, 2022

Comments

  • MEMS
    MEMS almost 2 years

    I was reading, and I saw the following code:

    template <>
    inline bool is_empty(const char* const& x)
    {
        return x==0 || *x==0;
    }
    

    What does const char* const& x mean?

    I tried the following code to understand it:

    void f(const char* const& x) 
    {
        // whatever
    }
    
    void main()
    {
        char a = 'z';
        char &j = a;
        f(a);//error
        f(*a);//error
        f(&a);//fine
        f(j);//error
        f(&j);//fine     
        f('z');//error
    }
    

    It works only for f(&a) and f(&j).

    What does const char* const& x actually mean?

    • Kerrek SB
      Kerrek SB almost 8 years
      It's like f(const T&), where T = const char*.
    • Marc van Leeuwen
      Marc van Leeuwen almost 8 years
      Note that it is not a pointer to reference as the title says (no such thing exists), but a reference to a pointer.
  • Marcus Müller
    Marcus Müller almost 8 years
    it's not a constant ref, it's a reference to a const pointer.
  • starturtle
    starturtle almost 8 years
    Correct, sorry. Got confused about right-to-left const association near the end.
  • eerorika
    eerorika almost 8 years
    @MarcusMüller constant reference is the same thing as reference to constant. Even the standard talks about "const reference", even though reference to const might be a better description.
  • Tonny
    Tonny almost 8 years
    Could you fix line 2 in the 1st code-section? You use "a" in stead of "x". That's a bit confusing.
  • 0xFF
    0xFF almost 8 years
    Which means that what?
  • Loreto
    Loreto about 7 years
    Simple and clear. And the use of the 'const &' pattern makes f("hello"); also acceptable.

Related