pointer to const object and const pointer,
1.指向常量对象的指针,意思是指针所指向的 对象内容不能变(不能够通过指针赋值)。
2.常量指针,意思是指针内容不能变,指向对象a就不能改为指向对象b(指针不能指向别处)。
(简单来说就是const挨着哪个近,哪个就不能变。)
pointer to const object: const double *cptr; // cptr may point to a double that is const const double pi = 3.14; double *ptr = π // error: ptr is a plain pointer const double *cptr = π // ok: cptr is a pointer to const const int universe = 42; const void *cpv = &universe; // ok: cpv is const void *pv = &universe; // error: universe is const const pointer: int errNumb = 0; int *const curErr = &errNumb; // curErr is a constant pointer curErr = curErr; // error: curErr is const
还有个更恶心的,const pointer to a const object
指向常量对象的常量指针,尼玛两个(pointer and object)都不能变,都是const.
const double pi = 3.14159; // pi_ptr is const and points to a const object const double *const pi_ptr = π