const

来看下面一段代码:

#include <iostream>
using namespace std;
int main()
{
	const int MAX_NUM = 731;
	const string EMAIL = "x-lion@xxx.edu.cn";
//	MAX_NUM = 130;	// expression must be a modifiable lvalued

	int a, b;
	const int* p = &a;
//	*p = 5;		// expression must be a modifiable lvalued
	a = 5;		//OK
	p = &b;

	int* pp = new int;
	p = pp;
//	pp = p;		//a value of type "const int*" cannot be assigned to an entity of type "int *" 为什么? 因为我们倾向于不修改const int* 指向的值。
	pp = (int*) p;	//当然我们还是可以强制赋值的。

	int c;
	const int& d = c;
//	d = 99;		// expression must be a modifiable lvalued
	c = 99;

	return 0;
}

void MyPrintf(const char* p)
{
//	strcpy(p, "C++");	//argumrnt of type "const char*" is incompatible with parameter of type "char*"
	printf("%s", p);
}


顺利地阅读这段代码,const的有关基本问题就掌握咯! 不懂的就求求度娘咯。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值