MEMS
MEMS

Reputation: 647

Pointer to reference

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?

Upvotes: 26

Views: 16713

Answers (5)

Marcus M&#252;ller
Marcus M&#252;ller

Reputation: 36327

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.

Upvotes: 10

Karl Nicoll
Karl Nicoll

Reputation: 16419

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.

Upvotes: 12

edmz
edmz

Reputation: 8494

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.

Upvotes: 3

starturtle
starturtle

Reputation: 739

It should translate to "A reference to a constant pointer to constant character". Which means the referenced pointer can't be assigned a new target address (to point to) and the string it references must not be modified.

edited after comment. Consequences should remain the same though.

Upvotes: 0

Vittorio Romeo
Vittorio Romeo

Reputation: 93264

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.

Upvotes: 33

Related Questions