[C++] 常量指针不能作为右值赋值给非常量指针

#include<iostream>
using namespace std;
int main(){
	int b[4]={1,2,3,4};
	const int* a = b;
	int * c = a;//error: invalid conversion from 'const int*' to 'int*' 
        return 0;
}

原因很简单,因为常量指针是指向常量的指针,const是一个限定符,你给自己加上这个限定,不代表你可以给别人加上这个限定,因此,如果别人不是指向const的指针,你就不能强迫别人也指向const。

posted @ 2018-08-16 11:10  zengzhaocheng  阅读(1141)  评论(0)    收藏  举报