const 与c++指针 - 计算机学习指南 #46
Replies: 2 comments 1 reply
-
做个笔记/*
A const Pointer
*/
//------------------------------------------------------------
//constant pointer to a non-constant int
// This means that the pointer `p` is constant, i.e.,
// it cannot be reassigned to point to a different memory location. However, the value it points to can be modified. For example, you can change the value of `*p`,
// but you cannot make `p` point to a different memory location.
int * const p;
//------------------------------------------------------------
//------------------------------------------------------------
// non-constant pointer to a constant int
//which means that the value which pointer p point can not be changed because it is constant.
// But pointer p can point to another memory location.
// The Writing method below are same;
const int* p;
int const* p;
//------------------------------------------------------------
// constant pointer to a constant int
// which means that the pointer can not point to another memory location,at the same time,the value p points also can not be changed.
// The pointer and the value it points are all constant
const int* const p;
int const* const p;
//------------------------------------------------------------ |
Beta Was this translation helpful? Give feedback.
1 reply
-
const Iterators
Let's see the code #include <iostream>
#include <vector>
int main(void) {
std::vector<int> v{1,2,3,4,5};
const vector<int>::iterator itr = v.begin();
itr++; // compile wrong!
*itr = 15; //compile successfully!
return 0;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
const 与c++指针 - 计算机学习指南
计算机学习指南,这里有从网络上收集的计算机各种方面的知识技能,可供查阅和学习
https://cs.meowrain.cn/Blogs/meowrain/c%2B%2B/const%E4%B8%8Ec%2B%2B%E6%8C%87%E9%92%88/
Beta Was this translation helpful? Give feedback.
All reactions