【发布时间】:2011-04-14 05:58:38
【问题描述】:
下面的代码正在处理一个TYPE* const 指针。
struct D {
void Check ()
{
D* const p = new D; // 2nd test is "p = 0;"
cout<<"p = "<<p<<endl;
(D*&)p = new D;
cout<<"p = "<<p<<endl; // prints 0, "p = 0;" at declaration
}
};
int main ()
{
D o;
o.Check();
}
我的问题是,
- 如果您使用
0进行初始化,那么即使下次类型转换也不起作用。进行这种类型转换是未定义的行为吗? -
this指针也是TYPE* const类型,那为什么编译器不允许this做同样的操作呢?
【问题讨论】:
标签: c++ pointers constants this undefined-behavior