c++中const的作用是什么? c++中 const的作用是什么

C++\u4e2dconst\u4ec0\u4e48\u610f\u601d\uff1f

\u5728\u6807\u51c6C++\u4e2d\uff0c\u8fd9\u6837\u5b9a\u4e49\u7684\u662f\u4e00\u4e2a\u5e38\u91cf\uff0c\u7528\u6765\u4fee\u9970\u5185\u7f6e\u7c7b\u578b\u53d8\u91cf\uff0c\u81ea\u5b9a\u4e49\u5bf9\u8c61\uff0c\u6210\u5458\u51fd\u6570\uff0c\u8fd4\u56de\u503c\uff0c\u51fd\u6570\u53c2\u6570\u3002
const\u4f7f\u7528\uff08\u7c7b\u578b\uff09:
1\u3001\u7528\u4e8e\u6307\u9488\u7684\u4e24\u79cd\u60c5\u51b5:const\u662f\u4e00\u4e2a\u5de6\u7ed3\u5408\u7684\u7c7b\u578b\u4fee\u9970\u7b26.
int const*A;//A\u53ef\u53d8,*A\u4e0d\u53ef\u53d8
int*const A;//A\u4e0d\u53ef\u53d8,*A\u53ef\u53d8
2\u3001\u9650\u5b9a\u51fd\u6570\u7684\u4f20\u9012\u503c\u53c2\u6570:
void function(const int Var);//\u4f20\u9012\u8fc7\u6765\u7684\u53c2\u6570\u5728\u51fd\u6570\u5185\u4e0d\u53ef\u4ee5\u6539\u53d8.
3\u3001\u9650\u5b9a\u51fd\u6570\u8fd4\u56de\u503c\u578b.
const int function();//\u6b64\u65f6const\u65e0\u610f\u4e49
const myclassname function();//\u51fd\u6570\u8fd4\u56de\u81ea\u5b9a\u4e49\u7c7b\u578bmyclassname.
4\u3001\u9650\u5b9a\u51fd\u6570\u7c7b\u578b.
void function()const;//\u5e38\u6210\u5458\u51fd\u6570,\u5e38\u6210\u5458\u51fd\u6570\u662f\u4e0d\u80fd\u6539\u53d8\u6210\u5458\u53d8\u91cf\u503c\u7684\u51fd\u6570\u3002

\u6269\u5c55\u8d44\u6599\uff1a\u603b\u7ed3:
1\u3001\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\u4e0d\u53ef\u53d8
(char*) const pContent;
const (char*) pContent;
2\u3001\u6307\u9488\u6240\u6307\u5411\u7684\u5185\u5bb9\u662f\u5e38\u91cf\u4e0d\u53ef\u53d8
const (char) *pContent;
(char) const *pContent;
3\u3001 \u4e24\u8005\u90fd\u4e0d\u53ef\u53d8
const char* const pContent;
4\u3001\u5176\u4e2d\u533a\u522b\u65b9\u6cd5\uff1a
\u6cbf\u7740*\u53f7\u5212\u4e00\u6761\u7ebf\uff0c
\u5982\u679cconst\u4f4d\u4e8e*\u7684\u5de6\u4fa7\uff0c\u5219const\u5c31\u662f\u7528\u6765\u4fee\u9970\u6307\u9488\u6240\u6307\u5411\u7684\u53d8\u91cf\uff0c\u5373\u6307\u9488\u6307\u5411\u4e3a\u5e38\u91cf\uff1b
\u5982\u679cconst\u4f4d\u4e8e*\u7684\u53f3\u4fa7\uff0cconst\u5c31\u662f\u4fee\u9970\u6307\u9488\u672c\u8eab\uff0c\u5373\u6307\u9488\u672c\u8eab\u662f\u5e38\u91cf\u3002
5\u3001const\u4fee\u9970\u51fd\u6570\u53c2\u6570
const\u4fee\u9970\u51fd\u6570\u53c2\u6570\u662f\u5b83\u6700\u5e7f\u6cdb\u7684\u4e00\u79cd\u7528\u9014\uff0c\u5b83\u8868\u793a\u51fd\u6570\u4f53\u4e2d\u4e0d\u80fd\u4fee\u6539\u53c2\u6570\u7684\u503c(\u5305\u62ec\u53c2\u6570\u672c\u8eab\u7684\u503c\u6216\u8005\u53c2\u6570\u5176\u4e2d\u5305\u542b\u7684\u503c)\u3002\u5b83\u53ef\u4ee5\u5f88\u597d
void function(const int Var); //\u4f20\u9012\u8fc7\u6765\u7684\u53c2\u6570\u5728\u51fd\u6570\u5185\u4e0d\u53ef\u4ee5\u6539\u53d8(\u65e0\u610f\u4e49\uff0c\u56e0\u4e3aVar\u672c\u8eab\u5c31\u662f\u5f62\u53c2)
void function(const char* Var); //\u53c2\u6570\u6307\u9488\u6240\u6307\u5185\u5bb9\u4e3a\u5e38\u91cf\u4e0d\u53ef\u53d8
void function(char* const Var); //\u53c2\u6570\u6307\u9488\u672c\u8eab\u4e3a\u5e38\u91cf\u4e0d\u53ef\u53d8(\u4e5f\u65e0\u610f\u4e49\uff0c \u56e0\u4e3achar* Var\u4e5f\u662f\u5f62\u53c2)
\u53c2\u6570\u4e3a\u5f15\u7528\uff0c\u4e3a\u4e86\u589e\u52a0\u6548\u7387\u540c\u65f6\u9632\u6b62\u4fee\u6539\u3002
6\u3001\u4fee\u9970\u5f15\u7528\u53c2\u6570\u65f6\uff1a
void function(const Class& Var);//\u5f15\u7528\u53c2\u6570\u5728\u51fd\u6570\u5185\u4e0d\u53ef\u4ee5\u6539\u53d8
void function(const TYPE& Var); //\u5f15\u7528\u53c2\u6570\u5728\u51fd\u6570\u5185\u4e3a\u5e38\u91cf\u4e0d\u53ef\u53d8
7\u3001const \u4fee\u9970\u51fd\u6570\u8fd4\u56de\u503c
const\u4fee\u9970\u51fd\u6570\u8fd4\u56de\u503c\u5176\u5b9e\u7528\u7684\u5e76\u4e0d\u662f\u5f88\u591a\uff0c\u5b83\u7684\u542b\u4e49\u548cconst\u4fee\u9970\u666e\u901a\u53d8\u91cf\u4ee5\u53ca\u6307\u9488\u7684\u542b\u4e49\u57fa\u672c\u76f8\u540c\u3002
(1) const int fun1() \u8fd9\u4e2a\u5176\u5b9e\u65e0\u610f\u4e49\uff0c\u56e0\u4e3a\u53c2\u6570\u8fd4\u56de\u672c\u8eab\u5c31\u662f\u8d4b\u503c\u3002
(2) const int * fun2()
8\u3001\u8c03\u7528\u65f6 const int *
\u53c2\u8003\u8d44\u6599\uff1a
\u767e\u5ea6\u767e\u79d1\u2014\u2014CONST\uff08C\u4e2d\u7684CONST\uff09

CONST\uff1a\u4e00\u4e2aC\u8bed\u8a00\u7684\u5173\u952e\u5b57

const关键字至少有下列n个作用:
  (1)欲阻止一个变量被改变,可以使用const关键字。在定义该const变量时,通常需要对它进行初始化,因为以后就没有机会再去改变它了;
  (2)对指针来说,可以指定指针本身为const,也可以指定指针所指的数据为const,或二者同时指定为const;
  (3)在一个函数声明中,const可以修饰形参,表明它是一个输入参数,在函数内部不能改变其值;
  (4)对于类的成员函数,若指定其为const类型,则表明其是一个常函数,不能修改类的成员变量;
  (5)对于类的成员函数,有时候必须指定其返回值为const类型,以使得其返回值不为“左值”。例如:
const classA operator*(const classA& a1,const classA& a2);
  operator*的返回结果必须是一个const对象。如果不是,这样的变态代码也不会编译出错:

classA a, b, c;
(a * b) = c; // 对a*b的结果赋值
  操作(a * b) = c显然不符合编程者的初衷,也没有任何意义。

CONST:一个C语言的关键字



扩展阅读:c对象 ... c++编程 ... c++教程 ... const在c++中的作用 ... c++中<< ... c++中const的用法 ... c++中cout的用法 ... c++中static的作用和用法 ... c++中cin的用法 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网