0
void main() {
const int a = 10;
const int *b = &a;
int *c = const_cast <int*>(b);
*c = 5;
cout<<a<<" "<<*b<<" "<<*c<<endl; //10 5 5
cout<<&a<<" "<<b<<" "<<c<<endl; //same address
cout<<*(int*)&a<<" "<<*&a<<endl; //5 10
}
什麼使得類型轉換影響了這個? 存儲的值在哪裏?2個值在一個變量(const和const_cast)中C++
它是'int main',而不是'void main'。修改常量數據是未定義的行爲。別指望什麼。 – chris
當你知道引用數據不是const時,'const_cast'應該只用於(從不,heh)。即如果'a'不是**'const',那麼''''''''''''''指針的const_cast具有優點。正如你寫的,你違反了。 [看到它](http://ideone.com/cutUWq) – WhozCraig
你的問題並不清楚,所以很難確切地知道你要找什麼信息。 –