在c/c++中经常会使用到const修饰符,使用改修饰符则表示改变量不能修改。
有些人会举出反例,比如这样:
这个时候a就是6了。其实很好解释,虽然指向int类型变量的指针p是一个常量,是不可更改的,可是我们也没有更改p,我们所做的只是让p所指向的int变量a执行++运算,这样a就是6了。
但是这样就不可以:
上面的之常量指针的例子,下面看指向常量的变量指针。
总结下,其实这个问题很简单,就是指向常量的变量指针,和指向变量的常量指针的问题而已。const在类型前面说明是修饰类型的,意思是指向了一个常量类型,const在标识符前面说明这是一个常量,是不可改的。
另外类型和标识符名称前都有const的用法,意思就很简单的交代了,这个指针本身是常量,一旦初始话就不能再指向其他地方,而且他指向的也是一个常量,也不能通过取值操作符去修改指向的内容。
下面是const int * p的另外一种写法: int const * p,反正这两种写法都是指向常整型的普通指针,而不是常指针。
其实const最常用的是在函数的传参上,一般在向一个函数传递地址时,会加上const用来防止更改指向的值,避免函数难以排查的"污染问题"。
#include <iostream>
using namespace std;
void printArr(const int* ptr, int num)
{
int i = 0;
while (i < num)
{
cout << *ptr << ",";
ptr++;
//(*ptr)++; 所指向的内容不能被修改
i++;
}
cout << endl;
}
void printArr2(const int arr[], int num)
{
int i = 0;
while (i < num)
{
cout << arr[i] << ",";
// arr[i] ++; 因为arr是指向常量数组的 所以不能更改
i++;
}
cout << endl;
}
void printArr3(const int& pos, int num)
{
int i = 0;
while (i < num)
{
cout << *(&pos + i) << ",";
//pos++; 这是一个常量引用 他引用的值不能被改变
i++;
}
cout << endl;
}
int main()
{
int arr[5] = {1,3,2,5,7};
printArr(arr, 5);
printArr2(arr, 5);
printArr3(arr[0], 5);
return 0;
}
有些人会举出反例,比如这样:
int a = 5;
int* const p = &a;
(*p)++;
cout << a << endl;
int* const p = &a;
(*p)++;
cout << a << endl;
这个时候a就是6了。其实很好解释,虽然指向int类型变量的指针p是一个常量,是不可更改的,可是我们也没有更改p,我们所做的只是让p所指向的int变量a执行++运算,这样a就是6了。
但是这样就不可以:
int a[2] = {5,6};
int* const p = a;
p++; //不能改变指针指向的位置
cout << *p << endl;
上面的例子的本意是想让p指向数组a的第二个元素,可是p是常量,他指向的位置是不能改变的。int* const p = a;
p++; //不能改变指针指向的位置
cout << *p << endl;
上面的之常量指针的例子,下面看指向常量的变量指针。
int a[2] = {5,6};
const int* p = a;
p++; //移动成功 指向第二个元素6
a[1]++; //a[1]这时候是7了
(*p)++; //错误 因为p是指向常量的
cout << *p << endl;
const int* p = a;
p++; //移动成功 指向第二个元素6
a[1]++; //a[1]这时候是7了
(*p)++; //错误 因为p是指向常量的
cout << *p << endl;
总结下,其实这个问题很简单,就是指向常量的变量指针,和指向变量的常量指针的问题而已。const在类型前面说明是修饰类型的,意思是指向了一个常量类型,const在标识符前面说明这是一个常量,是不可改的。
另外类型和标识符名称前都有const的用法,意思就很简单的交代了,这个指针本身是常量,一旦初始话就不能再指向其他地方,而且他指向的也是一个常量,也不能通过取值操作符去修改指向的内容。
int a[2] = {5,6};
const int* const p = a;
(*p)++; //所指的内容不能被改变
p++; //所指的位置也不能改变
cout << *p << endl;
const int* const p = a;
(*p)++; //所指的内容不能被改变
p++; //所指的位置也不能改变
cout << *p << endl;
下面是const int * p的另外一种写法: int const * p,反正这两种写法都是指向常整型的普通指针,而不是常指针。
int a[2] = {5,7};
// int const* p = a; // 这样写等同于 const int* p = a; (在gcc里调试通过)
const int * p = a;
* p++;
cout << *p << endl;
// int const* p = a; // 这样写等同于 const int* p = a; (在gcc里调试通过)
const int * p = a;
* p++;
cout << *p << endl;
其实const最常用的是在函数的传参上,一般在向一个函数传递地址时,会加上const用来防止更改指向的值,避免函数难以排查的"污染问题"。
#include <iostream>
using namespace std;
void printArr(const int* ptr, int num)
{
int i = 0;
while (i < num)
{
cout << *ptr << ",";
ptr++;
//(*ptr)++; 所指向的内容不能被修改
i++;
}
cout << endl;
}
void printArr2(const int arr[], int num)
{
int i = 0;
while (i < num)
{
cout << arr[i] << ",";
// arr[i] ++; 因为arr是指向常量数组的 所以不能更改
i++;
}
cout << endl;
}
void printArr3(const int& pos, int num)
{
int i = 0;
while (i < num)
{
cout << *(&pos + i) << ",";
//pos++; 这是一个常量引用 他引用的值不能被改变
i++;
}
cout << endl;
}
int main()
{
int arr[5] = {1,3,2,5,7};
printArr(arr, 5);
printArr2(arr, 5);
printArr3(arr[0], 5);
return 0;
}

浙公网安备 33010602011771号