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