【发布时间】:2013-10-01 05:32:04
【问题描述】:
class Foo{
private:
int i;
public:
Foo(int a)
{
i = a;
}
int getI() {return i;}
};
int main()
{
Foo* f;
if(true)
{
Foo g(1);
f = &g;
}
cout << f->getI() << endl;
return 0;
}
在上面的代码中,Foo 类的 g 对象一旦退出 if 子句就会超出范围。那么当cout语句执行时,会打印1吗?
【问题讨论】:
-
恶魔从你的鼻子里飞出来——或者,未定义的行为。
标签: c++ object pointers scope out