我正在使用一些代码来消除变量的恒定性。
int *i = new int(202);
int *j = new int(402);
int *const iptr = i; // Constant pointer
//iptr = j ; // Not allowed. Constant pointer cannot point to another location.
(*iptr)++; // Allowed
const_cast<int *>(iptr) = j;
cout<< *iptr // prints 402
它按预期工作,但是当我尝试删除“this”指针的恒定性时,编译器不允许它,即它在 const_cast 语句下方显示波浪线。
class A
{
public:
A(A * obj)
{
const_cast<A *>(this) = obj;
}
};
当我将鼠标(我使用 VS2014)悬停在早期代码中的“this”和“iptr”上时,我可以看到类型相同,即<classname> *const
谁能解释一下引擎盖下发生了什么?
干杯,
萨克特