VC++ 2010的問題: const bool bNew = true;
const_cast<bool&>(bNew) = false;
if(bNew)//bNew is false here, but
{
int i = 0;//this line will be executed
}
爲什麼? 謝謝。
請注意,我用std::thread只是在錯誤得到可讀類型: int main() {
const int * first;
using deref = decltype(*first);
std::thread s = std::remove_const<deref>::type{}; // const int ???
std::thread s2 = de
我知道,在const int *ptr中,我們可以更改地址,但不能更改該值。即ptr將被存儲在讀寫部分(堆棧)中,並且對象或實體將被存儲在數據分段的只讀部分中。所以,我們可以改變指針ptr指向的地址,但不能改變常量的對象。 int main()
{
const int *ptr=500;
(*ptr)++;
printf("%d\n",*ptr);
}
輸出爲
我有一個情況,在這裏我想有一個地圖,不允許添加/初始化之後刪除鍵,但值被允許改變(因此我不能簡單地使地圖const)。即 /*semi-const*/ map<int,int> myMap = initMap();
myMap[1] = 2; // NOT OK, because potentially adds a new key
myMap.at(1) = 2; // OK