Reputation: 37
What is this exactly:
int * const & arg
Is this
By the way, const references does not make any sense to me. I thought you can not change references after they are declared...
Upvotes: 1
Views: 205
Reputation: 238401
Is this
a reference to an int pointer?
No, because there is const.
a reference to a const int pointer?
Yes. It is better to use the same order as the declaration except in reverse to be clear: A
reference to a const pointer to int.
& const * int
Compared to the original, non reverse order:
int * const &
a const reference to an int pointer?
a const reference to an const int pointer?
There is technically no such thing as "const reference".
Although often people use it to mean reference to const. With such interpretation, a const reference to an int pointer is same as a reference to const int pointer. Also, with such interpretation, const reference to const has meaningless repetition of const.
Upvotes: 1
Reputation: 1380
arg is a reference to an int pointer, where that pointer itself is constant.
When declaring arg, you will notice that (because it is a reference) it must be initialized, as references cannot reference nothing.
An easy way to see this is by using an example:
int x;
int* const ptr = &x;
// declare an integer pointer which is constant i.e. the address it stores does not change,
// and initialize this address to the address of x
int* const& arg = ptr;
// declare a reference variable called arg, which references an integer pointer which is constant,
// the same type that ptr is, and then make it reference our ptr variable
Since the const argument is after the "*" in your line of code, you are telling C++ that the pointer itself is constant. However, if we were to write either
const int* & arg = ...
or
int const* & arg = ...
we would be telling C++ that the pointer is pointing to data (in this case an integer) which is constant. Further, if we wrote the following
int const* const & arg = ...
we would be telling C++ that the address the pointer stores is constant, and that this address points to an integer which is also constant.
Hope this helped!
Upvotes: 0