struct Foo
{
Obj* pObj;
Foo() : pObj(NULL);
};
Obj* CreateObj()
{
//do some stuff and then
return new Obj; //obj is a class
}
int main()
{
Foo foo;
foo.pObj = CreateObj();
DoSomeOperationWithTheObj(foo.pObj);
//suppose foo is a monster that should be 'killed' or deleted now
delete foo.pObj;
foo.pObj = NULL;
//the question is can this pointer be 're-used' now like this:
foo.pObj = CreateObj(); //create another object
}
由于指针被删除,是不是有问题,再次使用它吗?
不要将指针与它“指向”的对象混淆。 – 2012-10-23 05:15:00
@pst你的意思是我应该做'delete * foo.pOBj'? –
是的,您可以将指针重新分配给新的/不同的对象 – mac