Const in C++

本文探讨了C++中常量(const)及其引用(const reference)的使用方式,包括其在迭代器、指针和类型定义中的应用。通过具体代码示例解释了不同情况下const关键字的作用,以及如何正确地使用const引用。
摘要由CSDN通过智能技术生成

Why const?

Take a look at this snippet of codes
	for(int i;i<512;i++){
		cout<<i<<endl;
	}

There is a problem with it. It is not evident diret from the codes what does the magic number 512 means. In addition to that, if this number appears 100 times in our program.80 times with this meaning and 20 times with another,how can we differ from them.So..

	int memory=512;
	for(int i;i<memory;i++){
		cout<<i<<endl;
	}

Now,the problems above are solved. But by doing so it takes a risk of the value of variable memory can be accidentially changed anywhere else in the program.So..

	const int memory=512;
	for(int i;i<memory;i++){
		cout<<i<<endl;
	}

We can define it with key word const to tell the compiler any assignment to this region of area which variable memory defined is not allowed.

Reference Const

Const reference is reference to const

Found an interesting thing, it is allowed
	    const int val1=10;
	    const int &val2=val1;
	    cout<<val1+1;  //output 11
	    return 0;

But not this one:

	    const int val1=10;
	    const int &val2=val1;
	    val1+=1;  //error
	    val2+=1;  //error
	    return 0;

It is logical that it is a const reference that may  refer to a const variable.

	    const int val1=10;
	    int &val2=val1; //error, an const int& expected

We can bind a const reference to a nonconst variable.An assignment from the side of nonconst is allowed, but from the side of const refecence is refused.

	    int val1=10;
	    const int &val2=val1; //allowed
	    val1+=1; // allowed
	    val2+=1; // error: assignment of read-only reference 'val2'



Const_iterator

iterator points to const element of the vector.

	vector<int> v1(10,4);
	for(vector<int>::const_iterator itr=v1.begin();itr!=v1.end();itr++){
		*itr+=1;  //compiler error
		cout<<*itr<<endl;
	}

Iterator which is itself const

	vector<int> v1(10,4);
	for(const vector<int>::iterator itr=v1.begin();itr!=v1.end();){
		*itr+=1;
		cout<<*itr<<endl;break;
		itr++;     //compile error
	}

Pointers and Const Qualifier(Idiom: You can only tighten the access right of a variable as it is first defined)

	const double pi=3.14;
	double other=1;
	const double *p=π   //pointer to const object pointed to const object,correct
	p=&other;              //pointer to const object pointed to nonconst object,also correct
	double *q;             //pointer to nonconst object
	q=&other;              //correct, can only point to nonconst object
	q=π                 //compile error, not const object


Const Pointer

	const double pi=3.14;
	double *const p=π         //pointer itself const
	const double *const p=π   // const pointer points to const object

Pointers & Typedef

Cast On Const

	const int i=5;
	const_cast<int>(i)=2;/error: invalid use of const_cast with type 'int', which is not a pointer, reference, nor a pointer-to-data-member type


	const int i=5;
	int *k;
	k=const_cast<int*>(&i);
	*k=4;
	cout<<*k;




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值