Pointer to reference

const qualifies a type such that modifying the object becomes ill-formed. The error messages that you get in each case should explain what's going on.

Commented Jun 9, 2016 at 9:57

Note that it is not a pointer to reference as the title says (no such thing exists), but a reference to a pointer.

Commented Jun 9, 2016 at 11:38

I was going to say that passing by reference seems useless, but it looks like it's a template specialization where the template accepts references in the general case.

Commented Jun 9, 2016 at 14:16

Rule of thumb: read type declarations right to left. So "const char* const& x" means "x is a reference (&) to a constant (second const) pointer (*) to a char which is constant (first const)". -- This, BTW, is why I prefer the equivalent "char const * const & x" (x is a reference to a constant pointer to a constant char). Or typedefs. typedefs are your friend.

Commented Jun 9, 2016 at 22:24

5 Answers 5

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.

answered Jun 9, 2016 at 9:51 Vittorio Romeo Vittorio Romeo 92.7k 36 36 gold badges 267 267 silver badges 436 436 bronze badges I think its "constant reference to constant pointer of char type" Commented Jun 9, 2016 at 11:21

@sagar: No, there is no such thing as a constant reference. By nature references are themselves constant (cannot be reseated) so adding "constant" before it is a pleonasm.

Commented Jun 9, 2016 at 11:34 @MarcvanLeeuwen yes you are write. I misinterpreted the declaration. Thank you for clarification. Commented Jun 9, 2016 at 11:54

@MarcvanLeeuwen yes there is. Constant reference is the same thing as reference to constant. Although, since it's the same thing, sagar's "correction" is a bit superfluous. Constant (or const) reference is used more in parctice (in my experience) than reference to const, although the latter is more descriptive and therefore better in this context, where clarity is of importance.

Commented Jun 9, 2016 at 11:56

@sagar . although, prepending constant to "reference to const" is indeed redundant and it's indeed a pointer to const char, so I don't mean to say that the misinterpreted declaration is correct.

Commented Jun 9, 2016 at 12:06

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; //  

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  

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.