在下面的代碼中,爲什麼返回的指針p被允許改變?如果f代替返回「const int *」,那麼編譯器會在行「int * p = f()」處抱怨類型不匹配錯誤。爲什麼它對「int * const」做同樣的事情?從函數返回「int * const」
btw:我知道代碼中有內存泄漏,但這與問題無關。
int * const f() {
return new int(23);
}
int main(){
int * p=f();
p= new int(35);
return 0;
}
非常相關:http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const –