据我理解,const
修饰符应该从右往左读。由此得出:
const char*
这是一个指针,它的char元素不能被修改,但指针本身可以被修改。
char const*
这是一个指向“可变”字符的常量指针。
但是,对于以下代码,我得到了以下错误:
const char* x = new char[20];
x = new char[30]; //this works, as expected
x[0] = 'a'; //gives an error as expected
char const* y = new char[20];
y = new char[20]; //this works, although the pointer should be const (right?)
y[0] = 'a'; //this doesn't although I expect it to work
那么...到底是哪个出了问题呢?是我的理解有误还是我的编译器(VS 2005)有问题?