Могу ли я повторно использовать указатель в этом случае?C++

Программы на C++. Форум разработчиков
Anonymous
Могу ли я повторно использовать указатель в этом случае?

Сообщение Anonymous »

Предположим:

Код: Выделить всё

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
}
Поскольку указатель был удален, нет ли проблем с его повторным использованием, верно?

Подробнее здесь: https://stackoverflow.com/questions/130 ... -this-case

Вернуться в «C++»