C++ const_cast强制去掉常量属性

时间:2014-02-23 17:37:42   收藏:0   阅读:1721
#include<iostream>

using namespace std;

const int* find(int val,const int* t, int n);

int main(){
	int a[]={2, 4, 6};
	int* ptr;//不是const类型

	ptr=const_cast<int*>(find(4, a, 3));//去掉const类型

	if(ptr == 0)
		cout<<"not found"<<endl;
	else
		cout<<"found: value= "<<*ptr<<endl;
	return 0;
}

const int* find(int val, const int* t, int n)
//确定val在数组中的位置,并以const指针返回其地址
{
	int i;
	for(i=0;i<n;i++)
		if(t[i] == val)
			return &t[i];
		return 0;//not found
}


原文:http://blog.csdn.net/slience_perseverance/article/details/19723931

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!