2012-05-22 235 views

回答

41

const限定符適用於直接左側的類型,除非左側沒有任何內容,則它適用於右側的類型。所以,它是一樣的。

9

人爲的例子:

std::vector<char*> test; 
const auto a = test[0]; 
*a = 'c'; 
a = 0; // does not compile 
auto const b = test[1]; 
*b = 'c'; 
b = 0; // does not compile 

兩個ab具有類型char* const。不要以爲你可以簡單地「插入」類型而不是關鍵字auto(這裏:const char* a)! const關鍵字將應用於auto匹配的整個類型(這裏是:char*)。

相關問題