c++中的 关键字const 使用方法及说明
来源:互联网 发布:郑州大学网络登录 编辑:程序博客网 时间:2024/06/06 00:00
const定义的 本质:只读变量,即不可修改的变量。所以,它永远不可能是常量。
用法及说明:(想要的都有,不分类,自己看)
const int i // i 是 const
int cost i // i 是 const
const int* i // *i 是const,i不是
int const *i //*i 是const,i不是
int * const i //i是const,*i不是
const int* const i //i和*i都是const
int const * const i //i和*i都是const
int const *p1,p2 //*p1是const,p2是const
int *const p1,p2 //p1是const,p2不是
int const * const p1,p2 //*p1是const ,p1是const,p2是const
void function(int const * i) //参数*i为const,此参数在函数内不可修改
void function(int * const i) //参数i为const,此参数在函数内不可修改
void function(const class & t)//引用参数t为const,此参数在函数内不可修改
const int function()//无意义,给函数返回一个值的过程本身就是赋值
const int * function()//返回一个可修改的指针地址,但指针指向的内容不可更改,调用函数时,执行const int*ptr=fuction(),跟const修饰变量一个用法。
int *const function()//返回一个不可修改的指针地址,但指针指向的内容可以修改,调用函数时,执行int*const ptr=function()。
class AAA
{
const int count; //【一、const修饰成员变量】const成员变量不能被修改,只能在初始化列表中赋值
void cxh(int i):count(i){}; //:后面这叫做初始化列表
void func1();
void func2() const; //【二、const修饰成员函数】它不改变类的成员变量,也不能调用非const成员函数。
}
const AAA aaa; //【三、const修饰类对象】定义了一个静态对象,只能调用const成员函数。
aaa.func1(); //不能调用非const成员函数,是错的
aaa.func2(); //可以调用const成员函数
- c++中的 关键字const 使用方法及说明
- C语言中const关键字使用方法及使用场景
- C语言const介绍(const关键字说明)
- c/c++中的const关键字
- 理解C中的const关键字
- C语言中的关键字const
- C语言中的const关键字
- c语言中的关键字const
- c语言中的const关键字
- C语言关键字及说明
- 有关C语言中的关键字const
- 解析C与C++中的关键字const
- 详解C和C++中的const关键字
- C/C++中的关键字:static 和const
- C和C++中的const关键字
- C++ const关键字说明
- php面向对象类中的$this,static,final,const,self及双冒号 :: 这几个关键字使用方法。
- C/C++中关于CONST关键字的一点说明
- [数据结构暴力] zoj 3749 Chameleon
- 进程的优先级
- 诡异的dp
- 优先队列与循环队列
- Java 中的异常
- c++中的 关键字const 使用方法及说明
- [POJ 3468] A Simple Problem with Integers [线段树]
- Tempter of the Bone HDU 1010
- 软件架构模式的种类
- Setting up Minicom in Ubuntu
- 每日一C++小程序(十三)--矩形类求面积
- Linux进程调度策略
- IOS中类和对象还有,nil/Nil/NULL的区别
- [POJ 3468] A Simple Problem with Integers [线段树-动态建树版]