Pointers C++

Pointer to const 常量指针

int a = 10;
int b = 5;
const int *p = &a;
p = &b; //✅
// ilegal🚫: *p = b;
1
2
3
4
5

Const pointer 指针常量

int a = 10;
int b = 5;
int * const p = &a;
*p = 2; //✅
*p = b; //✅
// ilegal🚫: p = &b;
1
2
3
4
5
6

Const pointer to const

int a = 10;
int b = 5;
const int * const p = &a;
// ilegal🚫: *p = 2;
// ilegal🚫: *p = b;
// ilegal🚫: p = &b;
1
2
3
4
5
6
Last Updated: 7/23/2023, 7:19:36 AM
Contributors: oddnaveed