【发布时间】:2010-09-23 15:45:19
【问题描述】:
可能重复:
Why am I able to make a function call using an invalid class pointer
class B
{
public:
int i;
B():i(0){}
void func1()
{
cout<<“func1::B\n”;
}
void func2()
{
cout<<“i = “<<i;
}
};
int main()
{
B *bp = new B;
bp->func1();
delete bp;
bp = NULL;
bp->func1();
bp->func2();
return 1;
}
输出:
func1::B
func1::B
Runtime Exception:
NULL pointer access
【问题讨论】:
-
代码无法编译:没有包含,
main()被错误声明。它确实 NOT 什么都不返回;它返回一个int。 -
哇,它变成了社区维基。
-
显式取消引用 NULL ptr 有什么有趣的地方?
-
int main仍然是无效的方法声明/语法错误。 -
不,
int main()是标准的。编辑:我认为之前的评论是在抱怨之前的修订中缺少括号。