2016-09-30 119 views
0
uint8_t* buf1; 
... 
const signed char* buf2 = static_cast<const signed char*>(buf1); 

invalid static_cast from type 'uint8_t* {aka unsigned char*}' to type 'const signed char*'錯誤:從「無符號字符*」無效轉換「常量符號字符*」

C風格的鑄造:(常量符號字符*)工作正常

有沒有用c任何危險-style-cast vs static_cast在這種情況下?

+0

那麼,定義*「危險」*。它*是*合法的。 –

+0

static_cast錯誤提示這不是安全操作,爲什麼static_cast失敗? –

+0

,因爲你需要'reinterpret_cast' –

回答

0

Is there any danger using c-style-cast vs static_cast in this case?

static_cast在這種情況下根本就不是一個選項,正如錯誤信息所解釋的那樣。

使用c-style轉換的危險在於,您可能無意執行reinterpret_cast,這是c-style轉換在此處執行的操作。如果您打算執行reinterpret_cast,請使用reinterpret_cast。如果你打算使用static_cast,那麼你的邏輯是錯誤的,因爲這些類型與static_cast不兼容。

相關問題