const的用法是面试的时候,面试官的常问问题,下面给了最详细和容易理解的总结:
补充:常量指针 VS 指针常量
口诀:
(1)谁在前谁不能变,谁在后可以变=指针修饰谁,谁不能变
(2)左定值,右定向
-----------------------------分割线-----------------------------------------
下面是详细的解释,若上面的总结没有解决您的问题,继续看吧!
口诀:“左定值,右定向”
#include<bits/stdc++.h>
using namespace std;
/*******
左定值,右定向
********/
void test(){
//常量指针
int a=3;
const int *p=&a;
int b=2;
p=&b;
cout<<*p<<endl;
}
int main(){
//指针常量
int a=1;
int *const p=&a;
//int b=2;
// p=&b; 报错
cout<<*p<<endl;
test();
return 0;
}
4.const 对象 const Point p,只能调用带有const关键字的成员函数
常量指针 VS 指针常量
谁在前谁不能变,谁在后可以变=指针修饰谁,谁不能变