As title
剛剛寫程式的時候發現的一個問題
void p0(const int){}
void p1(const int*){}
void p2(const int**){}
int main()
{
int ptr0;
int *ptr1;
int **ptr2;
p0(ptr0);
p1(ptr1);
p2(ptr2);
return 0;
}
error: invalid conversion from int** to const int**
為什麼無法轉換啊
看起來明明是個很合理的轉換
或是有什麼情況會導致這個轉換出問題嗎?
作者:
lf5471 (lf)
2015-02-07 15:48:00g++ a.cpp -fpermissive 可以從 error 降成 warning
作者:
Feis (永遠睡不著 @@)
2015-02-07 16:35:00#1ItZvO1L (C_and_CPP)
作者:
OPIV (Monitor)
2015-02-07 16:49:00換成這樣就合法了int *const *p2c-faq.com/ansi/constmismatch.html
作者:
akasan (KITO)
2015-02-07 17:10:00#1B_2w2Uj
所以意思就是 p2 有 const,但是他可能是藉由沒有const 的轉上來的,導致了可以亂改他 deref 的結果
作者:
OPIV (Monitor)
2015-02-07 18:00:00是啊 只是你是把它pass到另一個函數裡,應該是不會被亂改到還是有什麼方法可以改 我不知道的?
作者:
Feis (永遠睡不著 @@)
2015-02-08 12:11:00只要有全域或另外傳入就可能爆炸。
函示宣告成void p2(int const*const*)比較適合情境喔
作者:
OPIV (Monitor)
2015-02-08 20:07:00那為什麼 const int 和 const int * 就可以?這樣另外傳或有全域也一樣暴啊!
作者:
avhacker (我想把整片天空打開)
2015-02-08 23:10:00