const 的作用
来源:互联网 发布:北京大学 奖教金 知乎 编辑:程序博客网 时间:2024/05/18 02:02
const是C++ 的一个关键字,被它修饰的变量或对象不允许被改变,所以可以提高程序的安全性和可靠性。
作用:
1.修饰常量:
const int a = 10;
a=20;(error)
2.修饰指针:
int a = 10;
const int* b = &a; 指针指向的内容不变
a = 20;(error)
int * const b = &a; 指针的内容不变
3.修饰函数的参数:
void func(const int & n) 参数在函数体内不能改变
{ n = 10 ;} (error)
4.修饰函数的返回值: 函数返回值不能变
const int *func() {};
int* a = func(); (error)
const int * a = func();
5. 修饰类的成员变量, 只能在构造函数初始化列表中赋值,
class A;
A(int x):a(x);
6. 修饰类的成员函数:
int &getValue() const; 该函数体内不能改变该类对象的任何成员变量,不能调用类中任何非const成员函数.
阅读全文
0 0
- const 的作用
- const的作用
- Const的作用
- const的作用
- const的作用
- const的作用
- const 的作用
- const的作用
- const的作用
- Static,Const的作用
- const的作用
- const的作用
- const的作用
- const的作用
- const的作用
- const关键字的作用
- const的作用
- const关键字的作用
- 使用nsupdate命令时报错:update failed: SERVFAIL
- bcmath.so
- Redis之集群redis-trib.rb环境的搭建-yellowcong
- 使用mybatis-generator-core生成MyBatis Mapper文件
- Javascript设计模式
- const 的作用
- 记录网站的登陆成功人数(servlet contex和config)
- Idea搜索不到项目文件
- 对Redis的理解,Redis是什么,Redis和Memcache谁快?
- Linux安装tomcat
- 选择输入控件,既可以输入又可以选择的控件
- 线性表
- linux 配置ip地址(6.9)
- GUI(图形用户界面)