Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
请向我解释为什么我不能通过使用 next 来改变常量?
const int i = 10; int * p = reinterpret_cast<int *>(&i);
您应该使用const_castto cast away constness,const_cast专门针对这种情况。
const_cast
const 表示恒定,因为您无法更改它。你可以做一个int nonconst_i = const_cast<int>(i);然后使用 nonconst_i
int nonconst_i = const_cast<int>(i);