container
개체와 item
개체가 있습니다. Item
은 container
의 일부입니다. item
멤버 함수는 item
을 삭제하는 container
함수를 호출합니다.구성원 함수에서 개체 삭제
container
item
개체가 item
구성원 기능을 삭제 한 기능을 수행하면 어떻게됩니까? 그것은 정의되지 않은 행동으로 이어지는 것처럼 들립니다. 이것은 delete this;
의 좀 더 정교한 사례입니까?
편집 :
class Item
{
Container* itemContainer;
std::string itemName;
void check(void)
{
bool condition = false;
// check some condition (not relevant to the question)
if (!condition)
{
itemContainer->CheckFailed(itemName);
}
}
}
class Container
{
std::vector<Item*> itemList;
void checkFailed(std::string)
{
Item* targetItem;
//find item by name
delete targetItem;
}
}
그래서 제 질문했다 : condition
이 거짓과 Container
에서 checkFailed
이 (targetItem
가 Check()
함수가 호출되는 곳에서 item
이다)라고하면 어떻게되는지.
몇 가지 코드로 상황을 예시 할 수 있습니까? 이 사실이 훨씬 더 명확 해집니다. – NathanOliver
https://isocpp.org/wiki/faq/freestore-mgmt#delete-this –