For pointers, the top-level qualifier const is also discarded and ignored.
For example, these two ads in the demo program declare the same function.
#include <iostream> void f( int * ); void f( int * const ); void f( int *p ) { std::cout << "*p = " << *p << std::endl; } typedef int *Ptr; int main() { int x = 10; int *p1 = &x; int * const p2 = &x; const Ptr p3 = &x; f( p1 ); f( p2 ); f( p3 ); return 0; }
Output to console:
*p = 10 *p = 10 *p = 10
The fact is that the argument to the function is passed by value. That is, a copy of the argument is made. Therefore, it does not matter whether the source object has a qualifier const or not. Its value is used only to initialize the function parameter, which is a local variable of the function.
The initialization of the parameter for each function call is as follows.
int *p = p1; int *p = p2; int *p = p3;
If the value of the parameter is changed in the function, it will not affect the original argument. That is, the original object that was passed as an argument to the function will remain unchanged, regardless of whether it has a const qualifier or not.
Another thing is when a pointer points to constant data. Then you need to distinguish whether you can change the data pointed to by the pointer, or can not.
That is, when accessing data through a pointer, you are working not with a copy of the data, but with the data itself.
By the way, if you pass a pointer by reference, that is, you do not pass a copy of the argument to the function, but, in fact, the argument itself, then there is already an overload.
The following program demonstrates this.
#include <iostream> void f( int * & ); void f( int * const & ); void f( int * & ) { std::cout << "int * &" << std::endl; } void f( int * const & ) { std::cout << "int * const &" << std::endl; } typedef int *Ptr; int main() { int x = 10; int *p1 = &x; int * const p2 = &x; const Ptr p3 = &x; f( p1 ); f( p2 ); f( p3 ); return 0; }
Its output to the console:
int * & int * const & int * const &
For simplicity, consider the following example. Let there be two declarations, one of which declares a constant object.
int x = 10; const int y = 20;
Then when using a view function
void f( int x );
You can pass any object declared above as an argument, since the function will only deal with a copy of the values of these objects.
If you have a function declared as
void f( int & );
or
void f( int * );
then you can use only the first declared object as an argument for these functions, such as
f ( x ); f( &x );
The function can change the object referenced by its parameter, and the object itself is not constant, it can be changed.
But you cannot transfer to this function the second declared object, that is how it is constant. Its program loader can generally be placed in a read-only memory.
Therefore, the second object can be used in functions where the parameter refers to constant data, as, for example.
void f( const int & );
or
void f( const int * );
Then you can write
f ( y ); f( &y );
The presence of the const qualifier means the obligation of the function not to change the object to which its parameter refers.
Also note the ad
const int * const
This ad can be represented as
const int ( * const )
You can, for example, write
int ( * const p ) = &x;
The const qualifier in parentheses is the top-level qualifier. It means that the pointer itself is constant.
A qualifier outside parentheses is the qualifier of the object referenced by the pointer.
For links, you cannot write in the same way:
const int & const
Links themselves are not constant (although for simplicity they often say a constant link, which means a link that refers to a constant object). These are the objects they refer to as constant.
f_numis identical tof_ptr, but must be different. More specifically: do you have anint xorconst int &xparameter? It's hard to answer without seeing the code you want to write. - Mark Shevchenkovoid f_ptr(const int* x)? Did you meanvoid f_ptr(int* const x)? - VladD