#include <iostream>
#include <list>
class Apple
{
private:
static std::list <Apple *> applePtrVector;
std::list <Apple *>::iterator iterator;
void Destroy();
public:
Apple();
~Apple();
staticvoid DestoryAll();
};
void Apple::Destroy()
{
deletethis;
}
Apple::Apple()
{
applePtrVector.push_front(this);
this->iterator = applePtrVector.begin();
}
Apple::~Apple()
{
applePtrVector.erase(this->iterator);
}
void Apple::DestoryAll()
{
for (std::list <Apple *>::iterator i = applePtrVector.begin(); i != applePtrVector.end(); i++)
{
(*i)->Destroy();
}
}
std::list <Apple *> Apple::applePtrVector;
int main()
{
new Apple;
new Apple;
new Apple;
new Apple;
Apple::DestoryAll();
return 0;
}
On the first call of 'delete this' I get the error "list iterator not incrementable". I realise I could use applePtrVector.clear but what if I needed to call 'delete this' in an update function?