Apologies for this, I am a student trying to learn C++ and I just thought it'd be better if I ask this question and gets many correct views on this so I am sorry for asking silly questions. I just figured it's better to ask than not know.
I understand what the address operator &
and the const
keyword mean separately. But when I was reading somewhere sample code I saw const&
used and just wanted to what this implied?
It was used in place of an argument in a function prototype, for example:
void function (type_Name const&);
If only one of them were used I would understand but I am a little confused here so some professional insight would be great.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it.
The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.
The const member functions are the functions which are declared as constant in the program. The object called by these functions cannot be modified. It is recommended to use const keyword so that accidental changes to object are avoided. A const member function can be called by any type of object.
const int* const says that the pointer can point to a constant int and value of int pointed by this pointer cannot be changed. And we cannot change the value of pointer as well it is now constant and it cannot point to another constant int. Thumb rule is to naming syntax from right to left.
Types in C++ are read right to left. This is, inconveniently just the opposite direction we like to read the individual words. However, when trying to decide what qualifiers like const
or volatile
apply to, putting the qualify always to the right make it easier to read types. For example:
int* const
is a const
pointer to a [non-const
] int
int const*
is a [non-const
] pointer to a const int
int const* const
is a const
pointer to a const int
For whatever unfortunate accident in history, however, it was found reasonable to also allow the top-level const
to be written on the left, i.e., const int
and int const
are absolutely equivalent. Of course, despite the obvious correct placement of const
on the right of a type, making it easy to read the type as well as having a consistent placement, many people feel that the top-level const
should be written on the left.
In this context, &
is not an address-of operator.
void function (type_Name const&);
is equivalent to:
void function (const type_Name &);
which is nothing but prototype of function taking argument by const
reference. You should study this kind of topics on your own, ideally from some good book. However, if you're looking for some additional material, these questions might help you too:
What are the differences between a pointer variable and a reference variable in C++?
Pointer vs. Reference, When to use references vs. pointers, Pass by Reference / Value in C++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With