C++之const

有点忘了:

#include "stdafx.h"
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
	printf("hello,world");
	///指向const对象的指针,const:翻译成只读的更好
	const double *cptr;
	//*cptr=40;              ///error
	const double pi=3.14;
	cptr=π                ///ok
	const void *cvptr=π
	//void *vptr=π        ///error

	double dval=3.14;        ///dval is a double; its value can be changed
	cptr=&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;
}
困。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值