C++之const

来源:互联网 发布:lc标签打印软件 编辑:程序博客网 时间:2024/06/05 11:40

有点忘了:

#include "stdafx.h"#include <stdlib.h>int _tmain(int argc, _TCHAR* argv[]){printf("hello,world");///指向const对象的指针,const:翻译成只读的更好const double *cptr;//*cptr=40;              ///errorconst double pi=3.14;cptr=π                ///okconst void *cvptr=π//void *vptr=π        ///errordouble dval=3.14;        ///dval is a double; its value can be changedcptr=&dval;              ///ok: but can't changed dval through cptr//*cptr=6.28;            ///error//不能保证指向const的指针所指对象的值一定不能修改///const指针:定义时必须初始化int errNumber=0;//int *const curError;   ///error:“curError”: 如果不是外部的,则必须初始化常量对象int *const curErr=&errNumber;//curErr=curErr;         ///error///curErr是指向int型对象的const指针system("pause");return 0;}
困。

原创粉丝点击