【发布时间】:2012-04-04 21:16:36
【问题描述】:
所以我很好奇以下代码崩溃的原因。 将不胜感激。
#include <iostream>
using namespace std;
class temp
{
public:
temp(int i)
{
intPtr = new int(i);
}
~temp()
{
delete intPtr;
}
private:
int* intPtr;
};
void f (temp fInput)
{
cout << "f called" << endl;
}
int main()
{
temp x = 2;
f(x);
return 0;
}
【问题讨论】:
-
@Ed S:我同意,但是,有时我们确定的崩溃可能与他认为的崩溃不同。
-
@josephthomas:是的,我也想这么多,这就是为什么我在发布后不久就删除了我的评论 :) 我们可以就该术语的正确使用达成一致......无论大多数初学者是否会使用这是另一个问题
-
使用
new需要正确管理内存。或者你可以不使用new而是使用整数而不是指向整数的指针。
标签: c++ class function pointers crash