c++基础之const
2017-04-01 22:00
155 查看
一、const与基本数据类型
1.const int x=3;//x不可更改
x=6;//✘
2.int x=4;
const int y=x;
y=5;//✘
二、const与指针
1.以下两种等价
const int *p=NULL;
int const *p=NULL;
注:*p不可更改//例如:*p=4;✘
p可更改//例如:p=&y;✔
2.以下两种等价
const int *const p=NULL;
int const *const p=NULL;
注:*p不可更改
p不可更改
三、const与引用
1.int x=3;
int *const p=&x;
p=&y;//✘
2.int x=3;const int &y=x;
x=10;//✔
y=10;//✘
3.const int x =3;
const int &y=x;
y=5;//✘
四、对比样例
1.const int x=5;
int *y=&x;//✘用一个可变的指针指向不可变变量是错误的。
2.int x=5;
const int *y=&x;//✔权限小的指向权限大的
1.const int x=3;//x不可更改
x=6;//✘
2.int x=4;
const int y=x;
y=5;//✘
二、const与指针
1.以下两种等价
const int *p=NULL;
int const *p=NULL;
注:*p不可更改//例如:*p=4;✘
p可更改//例如:p=&y;✔
2.以下两种等价
const int *const p=NULL;
int const *const p=NULL;
注:*p不可更改
p不可更改
三、const与引用
1.int x=3;
int *const p=&x;
p=&y;//✘
2.int x=3;const int &y=x;
x=10;//✔
y=10;//✘
3.const int x =3;
const int &y=x;
y=5;//✘
四、对比样例
1.const int x=5;
int *y=&x;//✘用一个可变的指针指向不可变变量是错误的。
2.int x=5;
const int *y=&x;//✔权限小的指向权限大的
相关文章推荐
- C++基础2:const类型
- c++基础:指针_引用_const
- C++基础之const系列
- c++基础 const 修改
- c++基础:新标准constexpr基本用法及其他注意点
- c++基础:新标准constexpr基本用法及其他注意点
- C++基础之访问const变量、访问私有变量and联合体(狗仔队)
- 【C++自我精讲】基础系列二 const
- 【C++自我精讲】基础系列二 const
- C++基础:const修饰符
- c++ const 变量基础
- [C++基础]令人头疼的const关键字
- c++基础:新标准constexpr基本用法及其他注意点
- c++基础:新标准constexpr基本用法及其他注意点
- c++ 基础 const char* 转 char*
- 【C++基础04】C/C++中的const
- C++的基础知识(二)—const
- C++基础知识--static成员和const成员函数
- C/C++基础面试-Const的全面理解(C部分)
- C++基础:#define宏定义和inline,const,enum的区别